error message question

C

Clay Forbes

When i click on mmy command button an error message comes up telling
me that i can only reference a control if it has focus. How can i fix
that?
Here is the code for that command button:

Private Sub Command11_Click()
On Error GoTo Err_Command11_Click


If Combo3.Text = "" Then
Label16.Visible = True
ElseIf Combo7.Text = "" Then
Label17.Visible = True
Else
DoCmd.GoToRecord , , acNewRec
End If
Exit_Command11_Click:
Exit Sub

What is wrong with that code that causes an error? How can i fix it?
 
F

fredg

When i click on mmy command button an error message comes up telling
me that i can only reference a control if it has focus. How can i fix
that?
Here is the code for that command button:

Private Sub Command11_Click()
On Error GoTo Err_Command11_Click

If Combo3.Text = "" Then
Label16.Visible = True
ElseIf Combo7.Text = "" Then
Label17.Visible = True
Else
DoCmd.GoToRecord , , acNewRec
End If
Exit_Command11_Click:
Exit Sub

What is wrong with that code that causes an error? How can i fix it?

You're referencing the control's Text property but you should (in
Access) be referencing the control's Value property. Since Value is
the default control property, you need not explicitly write it:

If Combo3 = "" Then
Label16.Visible = True
ElseIf Combo7 = "" Then
Label17.Visible = True
Else
DoCmd.GoToRecord , , acNewRec
End If

It might also be that you need to write it as:
If IsNull(Combo3) Then
etc.
ElseIf IsNull(Combo7) Then
etc.
End If
Whichever works in your situation.
--


Fred
Please only reply to this newsgroup.
I do not reply to personal email.
 
V

Van T. Dinh

Just use the Value Property rather than the Text Property. Since the Value
Property is the default Property of Controls, you don't even need to type
".Value". Your code should be something like:

If Len(Trim$(Me.Combo3 & "")) = 0 Then
Me.Label16.Visible = True
ElseIf Len(Trim$(Combo7 & "")) = 0 Then
Me.Label17.Visible = True
Else
DoCmd.GoToRecord , , acNewRec
End If

Note: I changed the conditions to check for BOTH Null and empty String.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top