PDA

View Full Version : Not Defined Sub



Jurodj
08-27-2007, 08:17 PM
I am testing how can I run code as an event in the lostfocus property of a control.

To do so, I am trying to run a simple code and the response I get when I try to run it in the inmediate screen is that the sub or function is not defined. The following code is what I tried to run.

Sub PET_ID_LostFocus()
Dim xBeeps, nBeeps As Integer
nBeeps = 10
For xBeeps = 1 To nBeeps
Beep
Next xBeeps
End Sub

I am a little confused here and I do not know what I have to do in order to access recognizes the code and run it in the lostfocus() property of the text control I am trying to implement it.
Pls help.

geekgirlau
08-27-2007, 08:33 PM
Welcome to the Board!

Is the control called "PET_ID"?

A simple way to add code to a control event is:

Select the control
Make sure Properties are displayed (View | Properties)
Go to the Event tab
Change the event (Lost focus) to [Event Procedure] from the drop-down list
Click on the build button (...)This takes you to the code window, and you insert your code there.

Jurodj
08-28-2007, 07:19 AM
:hi: Thank for your time. Let me explain the issue.
In a form I have two fields. One is custumer ID in the form AA### and the other is Pet_ID in the form AA###-##. For the Pet_ID field, the first 5 digits are the same as the custumer ID # and you assign -01, -02 etc to the pets a client may have.
What I want to do is to write a code that checks if the Pet_ID 5 first digits you enter in the field are the same as the 5 digits in the customer_ID. For that I went to the Pet_ID event tab and choose the "Code Builder" which sent me to the Vba Editor. I wrote the following code:
Sub pet_id_lostfocus()
If Left(PET_ID, 5) <> Left(CUSTOMER_ID, 5) Then
Dim Msg, Style, Title, Response 'Variables to use in the msgbox.
Msg = "5 first digits in Pet_ID must be the same as the CUSTOMER_ID" 'Defines the msg.
Style = vbRetryCancel 'Defines the type of button in the dialogbox.
Title = "Error in PET_ID!." 'Title of dialogbox.
Response = MsgBox(Msg, Style, Title)
If Response = vbRetry Then
DoCmd.CancelEvent
Else
DoCmd.Close
End If
End If
Beep
End Sub

The thing is that I ended up saving several modules that were empty and I deleted and now it do nothing when I enter data. If I go to create code again it always have the same code. In another case Access told me something about a missing reference which I do not know which one it is but since it does not run I think Access do not know how to link this code to the object event. A missing object library reference?. Please pardon me if I say something silly.
Regards,
Jurodj

OBP
08-28-2007, 11:40 AM
The code should not be necessary as the user should not be adding the ID numbers, they should be Autonumbers, to get the desired pet id add the Autonumber to the CustomerID.

Jurodj
08-28-2007, 02:33 PM
But I have noticed Access generates a number when you cancel an entry in a record thus you lose the sequence. If you enter data in a field and the press the "esc" key to cancel the entry, the autonumber function increments the number anyway. Is there a way you can prevent that, it would be easier to enter the data. Can you help me with the way I can achieve that?. Thanks.

OBP
08-29-2007, 02:42 AM
Why is that so important?
If you delete customers/Pets you will also get "gaps" in the sequence.
The only way that you can overcome the issue of Autonumbers being incremented and then cancelled is to use an "Unbound Form" for the data entry and only write the data to the table when the user clicks a Save or Close Button, but it a lot of work.

Jurodj
08-29-2007, 06:20 AM
:yesYes, I agree with you. But remember that you need a logical sequence for your data.
If you have:
1 JS001 John Smith
2 SS002 Sammy Sosa
3 AE003 Albert Einstein

You may delete for instance record #2, but in order to keep track of who were in record no. 2 you better desactivate it (mark it inactive), not delete it and it will not appear in your queries or filtered tables.

A different thing is to have records like:
4 AA004 Anika Astwood
5 PP123 Peter Parker
6 KM456 Ken Mattew
etc..