Generated Code From CMD Wizard

dynamix

Registered User.
Local time
Today, 23:11
Joined
Feb 9, 2005
Messages
38
Hey, this is probably a really easy question for some of you but as you will soon be able to tell, I am a complete rookie when it comes to VB.

Basically, I created a 'save record' button using the generator thing in Access. It generated the following on_click code:

Private Sub cmdProcessPayment_Click()
On Error GoTo Err_cmdProcessPayment_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_cmdProcessPayment_Click:
Exit Sub

Err_cmdProcessPayment_Click:
MsgBox Err.Description
Resume Exit_cmdProcessPayment_Click

End Sub

Now I want the following message box code to appear on the click event. I cn usually add this myself, but I am a little confused as it seems the on_click event has been coded already by the Access generated code. Anyway, the code I wanted to add is:

Private Sub Command8_Click()
Dim str As String
str = Forms![frmSubscriptions]![cboPlan]
MsgBox str

End Sub

Anyone can help me please?

PS, great forums :D
 
First of all, the wizards are out of date. They've been out of date for ten years and MicroSoft have never updated them for reasons known only to themselves.

Thus:

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

should be replaced with:

DoCmd.RunCommand acCmdSaveRecord


Your other three lines (i.e. these) *

Dim str As String
str = Forms![frmSubscriptions]![cboPlan]
MsgBox str

can be replaced simply with:

MsgBox Forms![frmSubscriptions]![cboPlan]

or:

MsgBox Forms("frmSubscriptions").cboPlan

The latter is my preference.


* Str is actually the name of a function and shouldn't be used as a variable name. It makes a great prefix for string variables, however, i.e. strForename.


As for the two lines in different command button codes - just copy the relevant lines into the other.


Code:
Private Sub cmdProcessPayment_Click()
    MsgBox Forms("frmSubscriptions").cboPlan
    DoCmd.RunCommand acCmdSaveRecord
End Sub


Incidentally, if frmSubscriptions is the current form - i.e. the form in which the code apppears on then you can reference it directly with the Me. keyword.

Code:
Private Sub cmdProcessPayment_Click()
    MsgBox Me.cboPlan
    DoCmd.RunCommand acCmdSaveRecord
End Sub
 
You can add the code into what is already there.

i.e. below where you have
Code:
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

just add

Code:
Dim str As String
str = Forms![frmSubscriptions]![cboPlan]
MsgBox str

I guess to keep your code tidy you should probably declare your variable (Dim str As String) at the start of the procedure instead of halfway through, but that won't affect how it works.
 
I was just pipped to the post, but by a much better answer. :rolleyes:
 
Thanks so much guys. Both were thourough indepth answers and my problem is now solved :). Thanks again!
 

Users who are viewing this thread

Back
Top Bottom