Tezcatlipoca
Registered User.
- Local time
- Today, 14:52
- Joined
- Mar 13, 2003
- Messages
- 246
I'm sure the answer to this is straightforward, but I've been hunting around on Access' help, these forums and elsewhere, and can't find any examples or solutions that fit with what I need.
Basically, my user is sitting on a form, frmSearchOnline. On this form are unbound text fields for MemberName and MemberNumber, allowing the user to freely enter whatever they like into these.
On the same form is a Save button (labelled btnAddMember), which has the following currently attached to it:
The validation check to ensure the Name and Number boxes have been filled works just fine, as does the code for my 'Move to Record?' query for the user. It is my validation check for duplicates (the bit in red text) that's failing.
When both boxes have been filled and the user clicks the Save button, the VB should hunt through the field MemberNumber in the linked table tblMembers for a match. If it finds an exact match with whatever the user has entered into the MemberNumber txt field, the user should receive a message that the number already exists and an offer to go to the existing record.
My first problem is with my DLookup not functioning properly, but I can't see why. I could instead go into the table tblMembers and simply make the MemberNumber field unique, but that's too sloppy for my needs, plus I need to retain control over what happens if a match is found.
My second problem is in actually creating the record if the match is clean. I think I need to use the SQL INSERT statement, but am not too sure of the syntax. In addition to tblMembers I have the linked table tblLogs. When a member is added with the Save button on the form, tblMembers should get a new row added with the contents of the Name and Number fields, and tblLogs should get a new row added with the contents of the Number field.
I'd naturally prefer to get direction on how I can teach myself where I'm going wrong, rather than have somebody supply the code for me, but my attempts to get this function working so far are proving fruitless.
Can anybody please advise?
Basically, my user is sitting on a form, frmSearchOnline. On this form are unbound text fields for MemberName and MemberNumber, allowing the user to freely enter whatever they like into these.
On the same form is a Save button (labelled btnAddMember), which has the following currently attached to it:
Code:
Private Sub btnAddMember_Click()
On Error GoTo Err_btnAddMember_Click
Dim stDocName As String
Dim strUserName As String
strUserName = Me.LoginName 'Ignore this bit. It's not relevant to my issues, and is used to pass a hidden username from one form to another
stDocName = "frmView"
If IsNull(MemberName) Then
MsgBox "You must enter a Name", vbExclamation, "Invalid Function"
Me.MemberName.SetFocus
Exit Sub
End If
If IsNull(MemberNumber) Then
MsgBox "You must enter a Number", vbExclamation, "Invalid Function"
Me.MemberNumber.SetFocus
Exit Sub
End If
If [COLOR="Red"]Me.MemberNumber.Value = DLookup("MemberNumber", "tblMembers")[/COLOR] Then
Select Case MsgBox("This number exists. Go to Record?" & vbCrLf, vbYesNo, "Record Select")
Case vbYes:
DoCmd.OpenForm stDocName, , , "[MemberNumber] = " & Me.MemberNumber, , acNone, strUserName
DoCmd.Close acForm, "frmSearchOnline", acSaveN
Case vbNo:
Exit Sub
Case Else:
Exit Sub
End Select
End If
'INSERT CODE WILL GO HERE
Exit_btnAddMember_Click:
Exit Sub
Err_btnAddMember_Click:
MsgBox Err.Description
Resume Exit_btnAddMember_Click
End Sub
The validation check to ensure the Name and Number boxes have been filled works just fine, as does the code for my 'Move to Record?' query for the user. It is my validation check for duplicates (the bit in red text) that's failing.
When both boxes have been filled and the user clicks the Save button, the VB should hunt through the field MemberNumber in the linked table tblMembers for a match. If it finds an exact match with whatever the user has entered into the MemberNumber txt field, the user should receive a message that the number already exists and an offer to go to the existing record.
My first problem is with my DLookup not functioning properly, but I can't see why. I could instead go into the table tblMembers and simply make the MemberNumber field unique, but that's too sloppy for my needs, plus I need to retain control over what happens if a match is found.
My second problem is in actually creating the record if the match is clean. I think I need to use the SQL INSERT statement, but am not too sure of the syntax. In addition to tblMembers I have the linked table tblLogs. When a member is added with the Save button on the form, tblMembers should get a new row added with the contents of the Name and Number fields, and tblLogs should get a new row added with the contents of the Number field.
I'd naturally prefer to get direction on how I can teach myself where I'm going wrong, rather than have somebody supply the code for me, but my attempts to get this function working so far are proving fruitless.
Can anybody please advise?
Last edited: