C
Carl Imthurn
Hello out there –
I need your help with a problem I can’t seem to solve. To wit:
I have a form (hereinafter ‘Form1’) with a number of combo boxes on it. I need all of them to have identical functionality: when I double-click on one of these combo boxes (hereinafter ‘Combo1’) I need the following to happen:
I need Form2 to appear on the screen. Form2 is a popup and modal form. It has a text box for a first name, a text box for a last name, an ‘Add person’ command button, and a ‘Cancel’ button. When I fill in a first name and a last name and click the ‘Add
person’ command button, I need a record to be added to a table.
So far so good – I have all of that working correctly. Here’s what I cannot figure out to save my life:
After the person is added to the database, I need the person’s name (formatted as LastName<comma space>FirstName to appear back in Combo1 on Form1 so that the user does not have to re-type in into the combo box.
Can’t figure it out. Spent more time than I care to admit on this one, so I could use some help. The error message I get is:
Microsoft Access can't find the form 'gForm' referred to in a macro expression or Visual Basic code.
Here’s the coding I have so far – like I said, everything works up to making the user input appear back in Combo1. I have added some in-line comments
Here’s the relevant code in the double-click event of Combo1 on Form1:
Private Sub Combo1_DblClick(Cancel As Integer)
AddNewPerson Me, Combo1
End Sub
Here’s the AddNewPerson subroutine:
Option Compare Database
Option Explicit
Public gFirstName As String
Public gLastName As String
Public gForm As Form
Public gComboBox As ComboBox
Public Sub AddNewPerson(frmIn As Form, cboIn As ComboBox)
' I need to pass in the form and the combo box because this functionality
' needs to be available to a number of combo boxes on a number of forms.
Set gForm = frmIn
Set gComboBox = cboIn
DoCmd.OpenForm "frmAddPerson"
End Sub
Here’s the relevant code in frmAddPerson:
Private Sub cmdAdd_Click()
On Error GoTo Err_cmdAdd_Click
lblMessages.Caption = ""
If Len(txtFirstName) = 0 Or IsNull(txtFirstName) Then
lblMessages.Caption = "Please enter a first name."
txtFirstName.SetFocus
Exit Sub
End If
If Len(txtLastName) = 0 Or IsNull(txtLastName) Then
lblMessages.Caption = "Please enter a last name."
txtLastName.SetFocus
Exit Sub
End If
gFirstName = txtFirstName
gLastName = txtLastName
Dim db As Database
Dim qdf As QueryDef
Dim strSQL As String
Set db = CurrentDb()
strSQL = "INSERT INTO tblPeople (FirstName, LastName) SELECT '" _
& gFirstName & "', '" & gLastName & "'"
Set qdf = db.CreateQueryDef("", strSQL)
qdf.Execute
' now update the control on the original form
Forms!gForm!gComboBox = gLastName & ", " & gFirstName
' ERROR ON PREVIOUS LINE – ERROR MESSAGE IS:
' Microsoft Access can't find the form 'gForm' referred to in a macro expression or Visual Basic code.
' done, so close the form
cmdExit_Click
Exit_cmdAdd_Click:
Exit Sub
Err_cmdAdd_Click:
MsgBox Err.Description
Resume Exit_cmdAdd_Click
End Sub
Private Sub cmdExit_Click()
On Error GoTo Err_cmdExit_Click
DoCmd.Close
Exit_cmdExit_Click:
Exit Sub
Err_cmdExit_Click:
MsgBox Err.Description
Resume Exit_cmdExit_Click
End Sub
Am I making this *way* more complicated than it has to be?
I don't know if I'm missing something embarrassingly simple . . . if so, please let me know.
As always, any and all help is gratefully accepted.
Thanks in advance –
Carl Imthurn
I need your help with a problem I can’t seem to solve. To wit:
I have a form (hereinafter ‘Form1’) with a number of combo boxes on it. I need all of them to have identical functionality: when I double-click on one of these combo boxes (hereinafter ‘Combo1’) I need the following to happen:
I need Form2 to appear on the screen. Form2 is a popup and modal form. It has a text box for a first name, a text box for a last name, an ‘Add person’ command button, and a ‘Cancel’ button. When I fill in a first name and a last name and click the ‘Add
person’ command button, I need a record to be added to a table.
So far so good – I have all of that working correctly. Here’s what I cannot figure out to save my life:
After the person is added to the database, I need the person’s name (formatted as LastName<comma space>FirstName to appear back in Combo1 on Form1 so that the user does not have to re-type in into the combo box.
Can’t figure it out. Spent more time than I care to admit on this one, so I could use some help. The error message I get is:
Microsoft Access can't find the form 'gForm' referred to in a macro expression or Visual Basic code.
Here’s the coding I have so far – like I said, everything works up to making the user input appear back in Combo1. I have added some in-line comments
Here’s the relevant code in the double-click event of Combo1 on Form1:
Private Sub Combo1_DblClick(Cancel As Integer)
AddNewPerson Me, Combo1
End Sub
Here’s the AddNewPerson subroutine:
Option Compare Database
Option Explicit
Public gFirstName As String
Public gLastName As String
Public gForm As Form
Public gComboBox As ComboBox
Public Sub AddNewPerson(frmIn As Form, cboIn As ComboBox)
' I need to pass in the form and the combo box because this functionality
' needs to be available to a number of combo boxes on a number of forms.
Set gForm = frmIn
Set gComboBox = cboIn
DoCmd.OpenForm "frmAddPerson"
End Sub
Here’s the relevant code in frmAddPerson:
Private Sub cmdAdd_Click()
On Error GoTo Err_cmdAdd_Click
lblMessages.Caption = ""
If Len(txtFirstName) = 0 Or IsNull(txtFirstName) Then
lblMessages.Caption = "Please enter a first name."
txtFirstName.SetFocus
Exit Sub
End If
If Len(txtLastName) = 0 Or IsNull(txtLastName) Then
lblMessages.Caption = "Please enter a last name."
txtLastName.SetFocus
Exit Sub
End If
gFirstName = txtFirstName
gLastName = txtLastName
Dim db As Database
Dim qdf As QueryDef
Dim strSQL As String
Set db = CurrentDb()
strSQL = "INSERT INTO tblPeople (FirstName, LastName) SELECT '" _
& gFirstName & "', '" & gLastName & "'"
Set qdf = db.CreateQueryDef("", strSQL)
qdf.Execute
' now update the control on the original form
Forms!gForm!gComboBox = gLastName & ", " & gFirstName
' ERROR ON PREVIOUS LINE – ERROR MESSAGE IS:
' Microsoft Access can't find the form 'gForm' referred to in a macro expression or Visual Basic code.
' done, so close the form
cmdExit_Click
Exit_cmdAdd_Click:
Exit Sub
Err_cmdAdd_Click:
MsgBox Err.Description
Resume Exit_cmdAdd_Click
End Sub
Private Sub cmdExit_Click()
On Error GoTo Err_cmdExit_Click
DoCmd.Close
Exit_cmdExit_Click:
Exit Sub
Err_cmdExit_Click:
MsgBox Err.Description
Resume Exit_cmdExit_Click
End Sub
Am I making this *way* more complicated than it has to be?
I don't know if I'm missing something embarrassingly simple . . . if so, please let me know.
As always, any and all help is gratefully accepted.
Thanks in advance –
Carl Imthurn