PC Review


Reply
Thread Tools Rate Thread

Adding code to an existing COMMAND BUTTON

 
 
kealaz
Guest
Posts: n/a
 
      15th Apr 2009
I have a command button that I am using on a form [frmPO_ISSUE]. This
command button is currently closing the form and opening a report. The
report is based on a query and the query is run using the strCriteria (which
comes from a text box on the form). The code is below. All of this is
working so far.


*********************************************
Private Sub ISSUE_PO_Click()
On Error GoTo Err_ISSUE_PO_Click

Const conREPORTNAME = "rptPO"
Dim strCriteria As String

strCriteria = " P_O_NO = """ & Forms("frmPO_ISSUE").P_O_NO & """"

' close form
DoCmd.Close acForm, "frmPO_ISSUE"

' open report in print preview
DoCmd.OpenReport conREPORTNAME, _
View:=acViewPreview, _
WhereCondition:=strCriteria

Exit_ISSUE_PO_Click:
Exit Sub

Err_ISSUE_PO_Click:
MsgBox Err.Description
Resume Exit_ISSUE_PO_Click

End Sub
***********************************************



I want to add the following code to the above command button. I have tried,
and it's not working. I have verified that the SQL query works. I just
don't know how to integrate it into the code I already have. The part that
it's not liking is "WhereCondition:=strCriteria" It works for opening
report/query to view the purchase order. I thought that I could use the same
method for another query. The SQL statement below is a delete query.
qryDeleteBUY_a is a select query.

******* code I want to add to the above ********

'save record
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

'run query
DoCmd.RunSQL "DELETE tblBUY.PART_NO FROM tblBUY WHERE PART_NO IN (SELECT
PART_NO FROM qryDeleteBUY_a);"
WhereCondition:=strCriteria
 
Reply With Quote
 
 
 
 
BruceM
Guest
Posts: n/a
 
      15th Apr 2009
If you are using Access 2000 or above you could make your life simpler by
saving a record thus:
Me.Dirty = False
I'm not sure exactly when Access got away from using only the horrid
DoMenuItem syntax, but it was at least Access 2000.

Regarding the SQL, the Where needs to be written into the SQL. You cannot
add a Where condition as with OpenForm.

Dim strSQL as String, strPO as String

strPO = Forms("frmPO_ISSUE").P_O_NO
or:
strPO = Forms!frmPO_ISSUE!P_O_NO
or if frmPO_Issue is the form on which the command button resides:
strPO = Me.P_O_NO

strSQL = "DELETE tblBUY.PART_NO FROM tblBUY WHERE PART_NO = " & _
strPO

DBEngine(0)(0).Execute strSQL, dbFailOnError

All of this assumes P_O_NO is text, which is what you have indicated by your
use of quotes in strCriteria.

The SQL is a suggestion. You would do best to create a delete query using
the SQL (substitute an actual value for strPO), run it, and see if you get
the expected results. I can't tell the details of your database from your
description. Is the current form based on tblBuy? If not, what is its
Record Source? BTW, are you sure you need to delete the record?

"kealaz" <(E-Mail Removed)> wrote in message
news:7431C424-088F-4C67-A02A-(E-Mail Removed)...
>I have a command button that I am using on a form [frmPO_ISSUE]. This
> command button is currently closing the form and opening a report. The
> report is based on a query and the query is run using the strCriteria
> (which
> comes from a text box on the form). The code is below. All of this is
> working so far.
>
>
> *********************************************
> Private Sub ISSUE_PO_Click()
> On Error GoTo Err_ISSUE_PO_Click
>
> Const conREPORTNAME = "rptPO"
> Dim strCriteria As String
>
> strCriteria = " P_O_NO = """ & Forms("frmPO_ISSUE").P_O_NO & """"
>
> ' close form
> DoCmd.Close acForm, "frmPO_ISSUE"
>
> ' open report in print preview
> DoCmd.OpenReport conREPORTNAME, _
> View:=acViewPreview, _
> WhereCondition:=strCriteria
>
> Exit_ISSUE_PO_Click:
> Exit Sub
>
> Err_ISSUE_PO_Click:
> MsgBox Err.Description
> Resume Exit_ISSUE_PO_Click
>
> End Sub
> ***********************************************
>
>
>
> I want to add the following code to the above command button. I have
> tried,
> and it's not working. I have verified that the SQL query works. I just
> don't know how to integrate it into the code I already have. The part
> that
> it's not liking is "WhereCondition:=strCriteria" It works for opening
> report/query to view the purchase order. I thought that I could use the
> same
> method for another query. The SQL statement below is a delete query.
> qryDeleteBUY_a is a select query.
>
> ******* code I want to add to the above ********
>
> 'save record
> DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
>
> 'run query
> DoCmd.RunSQL "DELETE tblBUY.PART_NO FROM tblBUY WHERE PART_NO IN (SELECT
> PART_NO FROM qryDeleteBUY_a);"
> WhereCondition:=strCriteria



 
Reply With Quote
 
Tom van Stiphout
Guest
Posts: n/a
 
      15th Apr 2009
On Wed, 15 Apr 2009 06:15:01 -0700, kealaz
<(E-Mail Removed)> wrote:

Perhaps you mean:
dim sql as string
sql = "DELETE tblBUY.PART_NO FROM tblBUY WHERE PART_NO IN (SELECT
PART_NO FROM qryDeleteBUY_a) AND " & strCriteria
DoCmd.RunSQL sql

Personally I like to run a parameter query, rather than use inline
SQL.

-Tom.
Microsoft Access MVP


>I have a command button that I am using on a form [frmPO_ISSUE]. This
>command button is currently closing the form and opening a report. The
>report is based on a query and the query is run using the strCriteria (which
>comes from a text box on the form). The code is below. All of this is
>working so far.
>
>
>*********************************************
>Private Sub ISSUE_PO_Click()
>On Error GoTo Err_ISSUE_PO_Click
>
>Const conREPORTNAME = "rptPO"
>Dim strCriteria As String
>
>strCriteria = " P_O_NO = """ & Forms("frmPO_ISSUE").P_O_NO & """"
>
> ' close form
> DoCmd.Close acForm, "frmPO_ISSUE"
>
> ' open report in print preview
> DoCmd.OpenReport conREPORTNAME, _
> View:=acViewPreview, _
> WhereCondition:=strCriteria
>
>Exit_ISSUE_PO_Click:
>Exit Sub
>
>Err_ISSUE_PO_Click:
>MsgBox Err.Description
>Resume Exit_ISSUE_PO_Click
>
>End Sub
>***********************************************
>
>
>
>I want to add the following code to the above command button. I have tried,
>and it's not working. I have verified that the SQL query works. I just
>don't know how to integrate it into the code I already have. The part that
>it's not liking is "WhereCondition:=strCriteria" It works for opening
>report/query to view the purchase order. I thought that I could use the same
>method for another query. The SQL statement below is a delete query.
>qryDeleteBUY_a is a select query.
>
>******* code I want to add to the above ********
>
>'save record
>DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
>
>'run query
>DoCmd.RunSQL "DELETE tblBUY.PART_NO FROM tblBUY WHERE PART_NO IN (SELECT
>PART_NO FROM qryDeleteBUY_a);"
>WhereCondition:=strCriteria

 
Reply With Quote
 
kealaz
Guest
Posts: n/a
 
      15th Apr 2009
Thanks Tom! This worked great!
 
Reply With Quote
 
kealaz
Guest
Posts: n/a
 
      15th Apr 2009
Thanks Bruce! I appreciate your post. I changed the save record code. Much
simpler.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Code to Command Button at Run Time =?Utf-8?B?S2V2aW4gRS4=?= Microsoft Excel Programming 1 20th Sep 2006 12:25 AM
Clear Search Fields code adding to an existing Command Button Igor via AccessMonster.com Microsoft Access Queries 1 2nd Jun 2006 01:48 PM
Adding Code to an Existing Command Button with VBA jasoncw Microsoft Excel Programming 1 15th Feb 2005 11:15 PM
Adding code to a command button Todd Microsoft Excel Misc 2 30th Aug 2004 02:39 PM
Adding a command button through code/macro? =?Utf-8?B?U29tYmE=?= Microsoft Access VBA Modules 1 17th Jul 2004 04:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:30 AM.