Need some help with my code/code syntax

Jdreyfus

Registered User.
Local time
Yesterday, 17:16
Joined
Jun 19, 2008
Messages
27
I've been messing around attempting to get a field in a form to notify the person entering information if a record already exists, and allow a quick link to edit said existing record. I'm having very little luck making everything work the way I believe it should. I have virtually no understanding of VB, so I've been getting advice along the way and inserting what I believe to be the right values into each section.

I'm posting a screen shot of the form I'm using and the code associated with it. Along with the error when executed/the highlighting after that.

AccessScreenshotmainform.jpg


VBErrormessage.jpg


VBErrormessageafterclick.jpg



Code:
Private Sub YC_TAG_AfterUpdate()
If DCount("YC_TAG", "Assets", _
"YC_TAG = " & Me.YC_TAG) = 0 Then
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
Else
If MsgBox("This asset already exists in the database. " & _
"Would you like to edit that record?", vbExclamation + vbYesNo) =vbYes
Then
DoCmd.SearchForRecord ([acDataForm = Me.Name, "YC_TAG", Me.Name = acFirst])
End If
End If
End Sub


I don't know enough to know why this isn't working, and have attempted to stumble upon a solution to no avail. I'm hoping it's something really obvious to you gurus. Thank you anyone that gives this some thought.
 
The compile error is due to the "Then", which should be at the end of the previous line. That's why it's in red.
 
Thank you, now I'm getting an error that's associated with this line:
DoCmd.SearchForRecord ([acDataForm = Me.Name, "YC_TAG", Me.Name = acFirst])
 
Runtime Error 2465: Access can't find the field '|' referred to in your expression
 
I'm not familiar with that, but looking at 2007 help it looks similar to a wherecondition, so try:

DoCmd.SearchForRecord "YC_TAG = " & Me.YC_TAG

That presumes YC_TAG is numeric; if not, the same rules apply as in that link I gave you recently.
 
That seems to be the right direction, it's now saying run time error 13 type mismatch. YC_TAG is numeric in terms of the data itself, but not numeric in terms of the data type because it leads with zeros.
 
Please rename your field something that is not a reserved word ("Name").

You have brackets around the entire contents of the parameters to SearchForRecord(). You need to fix that:
Code:
DoCmd.SearchForRecord (acDataForm = Me.[CorrectlyNamedField], "YC_TAG", Me.[CorrectlyNamedField] = acFirst)

I don't know if it'll fix it since I never use "SearchForRecord()" but it definitely won't work the way it was.

Indent your code!
 
I wasn't sure what the Me.Name was so I left it alone, what is should it be pointing to?
 
Try

DoCmd.SearchForRecord "YC_TAG = '" & Me.YC_TAG & "'"
 

Users who are viewing this thread

Back
Top Bottom