Object Required Error (1 Viewer)

Irenaught

New member
Local time
Today, 07:34
Joined
Jun 4, 2009
Messages
7
Well I solved the last one but now Im stumped again. The following code is in my functions module I am creating to check the data in the form is correct before inserting the new record.

Public Function CheckPhone(strCheckPhone As String)
If Len(strCheckPhone) <> 7 Then
MsgBox ("The Phone Number is Incorrect")
txtPhone.SetFocus
txtPhone.SelStart = 0
txtPhone.SelLength = Len(txtPhone)
End If
End Function

It gives an runtime error '424' object required on the "txtPhone.SetFocus" line. The message box pops up fine and everything. Any help would be great thank you.
 

jjturner

Registered User.
Local time
Today, 14:34
Joined
Sep 1, 2002
Messages
386
Did you try removing the parentheses from the MsgBox statement?

MsgBox "The Phone Number is Incorrect"

With parentheses, I believe Access interprets the statement as an expression to be calculated (i.e., as part of an 'If' statement)

Hope this helps

John
 

Irenaught

New member
Local time
Today, 07:34
Joined
Jun 4, 2009
Messages
7
I tried it and it doesn't make the difference... It actually seems to make no difference which is interesting. I think it doesn't know what form to find txtPhone on or something like that but Im not sure how to refer to it more directly... maybe i can find that on the forums somewhere... Let me know if you have any other ideas though and thanks for the thoughts.
 

___

¯¯¯¯¯
Local time
Today, 14:34
Joined
Nov 27, 2003
Messages
595
Make sure you have a reference set for Microsoft DAO 3.6 Object library (or higher), and call the function from an event on the form.
 
Last edited:

Kiwiman

Registered User
Local time
Today, 14:34
Joined
Apr 27, 2008
Messages
799
Howzit

If the Function is in a module, and not part of the form you are calling the function from, the txtPhone control does not exist as part of that object - it only exists on the form.

Try something like...

Code:
Public Function CheckPhone(strCheckPhone As String)
If Len(strCheckPhone) <> 7 Then
checkphone = false
else
checkphone = true
end if

End Function

Code:
YourForm_event_Sub
If CheckPhone("yourstring") = false then
  MsgBox "The Phone Number is Incorrect"
  txtPhone.SetFocus
  txtPhone.SelStart = 0
  txtPhone.SelLength = Len(txtPhone)
else

End If
End sub
 

Irenaught

New member
Local time
Today, 07:34
Joined
Jun 4, 2009
Messages
7
Here is the code i used that fixed it. I don't know if its the simplest but it worked. I did need to add a Boolean aspect to make the non module subroutine work as well.
Code:
Public Function CheckPhone(strCheckPhone As String)
Dim boolPhone As Boolean

If Len(strCheckPhone) <> 7 Then
MsgBox "The Phone Number is Incorrect"
[Forms]![Add Donor Form]![txtPhone].SetFocus
[Forms]![Add Donor Form]![txtPhone].SelStart = 0
[Forms]![Add Donor Form]![txtPhone].SelLength = Len([Forms]![Add Donor Form]![txtPhone])
Else
boolPhone = True
End If
End Function

- end code

and how do i make proper code boxes on this site? Thanks for all the help.
 

Kiwiman

Registered User
Local time
Today, 14:34
Joined
Apr 27, 2008
Messages
799
Howzit

To make proper code boxes select the Hash symbol (when Go Advanced is selected) #.

Yes your solution would work, but as it is used in a Public Function (is it in a separate Module?), and other objects can call it, when the string is <>7 then it will always try to set focus to the control in the form you have specified. What if you called it from another form? Or even another control

If it is only to be used for this form - it will work, but should problably form part of the After Update event of your control - in the form istself - not as a Public Function
 
Last edited:

Users who are viewing this thread

Top Bottom