View Full Version : Help with VBA code not working
HelenEvans
09-28-2011, 04:27 AM
Can you help why is my code not working or can you suggest improvemnts.
[VBA]
Private Sub Find_Click()
If IsNull(TxtFindName) = True Then
MsgBox "Dim enw cyfnod", 0, "search failed"
Exit Sub
AilEnw.SetFocus
DoCmd.FindRecord TxtFindName
If AilEnw <> chwilioenw Then
MsgBox "Heb Enw"
TxtFindName.SetFocus
Exit Sub
End If
[VBA]
hansup
09-28-2011, 11:07 AM
Welcome to the forum.
What symptoms do you see from the code not working?
Do you get an error message? If so, what is the message and which code line is highlighted when you click the Debug button on the error message dialog?
It seems you gave us only part of the code from the Find_Click() procedure. It should end with a "End Sub" statement. Also you have 2 If statements, but only one End If.
When writing your message here, it helps to use the VBA button (green text on white background) to format code like this:
Private Sub Find_Click()
If IsNull(TxtFindName) = True Then
MsgBox "Dim enw cyfnod", 0, "search failed"
Exit Sub
AilEnw.SetFocus
DoCmd.FindRecord TxtFindName
If AilEnw <> chwilioenw Then
MsgBox "Heb Enw"
TxtFindName.SetFocus
Exit Sub
End If
So looks to me like when TxtFindName is Null, your procedure will display that first message box, then terminate. What do you want instead?
And if TxtFindName isn't Null, the code won't do anything. Is that what you want?
Is this closer to what you wanted?
Private Sub Find_Click()
If IsNull(Me.TxtFindName) = True Then
MsgBox "Dim enw cyfnod", 0, "search failed"
'Exit Sub
Else ' TxtFindName is not Null
Me.AilEnw.SetFocus
DoCmd.FindRecord Me.TxtFindName
If Me.AilEnw <> chwilioenw Then
MsgBox "Heb Enw"
Me.TxtFindName.SetFocus
'Exit Sub
End If
End If
End Sub
I assumed TxtFindName and AilEnw are the names of (text box?) controls on your form. So I prefaced them with the Me keyword, which means "this form". That change is not strictly necessary but helps avoid confusion both for Access and human readers.
I didn't try to guess what chwilioenw is ... control, variable, field in the form's recordset (?).
HelenEvans
09-28-2011, 12:58 PM
Thanks I am a teacher trying to work this out from a bit of code I had off someone. I teach in welsh to add to the confusion. so I created a label and called it Textfindname but I have a field on the from Ailenw (Surname). Thats what I am trying to use as a search cirteria and return the result. Thank you for taking the time to help I know nothing of VBA I will try and work it out tommorow in School.
Thanks again.
HelenEvans
09-29-2011, 04:16 AM
It works thank you thank you Helen Evans
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.