M
Michelle Podgornik
This is a continuation of a previous question.
Briefly, here is the design of my database:
tblUsers
UserID
FirstName
LastName
Address
tblRequests
RequestID
UserID
RequestedDataFile
AccessStartDate
AccessEndDate
The relationship between the tables is one-to-many (a single user may have
more than one request). Both tables have associated forms which contain
pretty much the same info. The Requests form is a linked subform of the
Users form.
I am using the following code to find and update an existing record by
typing the UserID of an individual into the Users form. Here is how I want
it to work: If the UserID has never been entered, then I continue typing in
the form in order to create the record. If the UserID has been entered
before (i.e. a record already exists), then the pre-existing record is
displayed along with any info in the Requests subform. I can then update the
record by adding any new requests.
The code is not working. When I type in a UserID (existing or not), I get
the error "Method or Control not found." The debugger goes to the last
character of the line: RS.FindFirst "[" & C.ControlSource & "]=" & C
Any ideas?
Thanks!
Michelle
Option Explicit
Dim Found
Function Find_BeforeUpdate (F As Form)
Dim RS As Recordset, C As Control
Set C = Screen.ActiveControl
Set RS = F.RecordsetClone
On Error Goto Err_Find_BeforeUpdate
' Try to find a record with a matching value.
Select Case RS.Fields(C.ControlSource).Type
' Find using Numeric data type key value?
Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, _
DB_DOUBLE, DB_BYTE
RS.FindFirst "[" & C.ControlSource & "]=" & C
' Find using Date data type key value?
Case DB_DATE
RS.FindFirst "[" & C.ControlSource & "]=#" & C & "#"
' Find using Text data type key value?
Case DB_TEXT
RS.FindFirst "[" & C.ControlSource & "] = """ & C & """"
Case Else
MsgBox "ERROR: Invalid data type for '" & C.Name & "'!"
DoCmd.CancelEvent
Exit Function
End Select
' If a record is found, save the found record's bookmark.
If RS.NoMatch Then
Found = Null
Else
Found = RS.Bookmark
End If
' If the record is found...
' ...cancel the BeforeUpdate event
' ...undo changes made to the current record
' ...and TAB to the next control to trigger the OnExit routine.
If Not IsNull(Found) Then
DoCmd.CancelEvent
SendKeys "{ESC 2}{TAB}", False
End If
Exit Function
Err_Find_BeforeUpdate:
MsgBox "ERROR: Err " & Err & ": " & Error$, 48
DoCmd.CancelEvent
Exit Function
End Function
Function Find_OnExit ()
' If the record is found, cancel the OnExit routine to stay
' in the control and go find the record.
If Not IsNull(Found) And Len(Found) <> 0 Then
DoCmd.CancelEvent
' Synchronize the form record with the found record.
Screen.ActiveForm.Bookmark = Found
Found = Null
End If
End Function
Briefly, here is the design of my database:
tblUsers
UserID
FirstName
LastName
Address
tblRequests
RequestID
UserID
RequestedDataFile
AccessStartDate
AccessEndDate
The relationship between the tables is one-to-many (a single user may have
more than one request). Both tables have associated forms which contain
pretty much the same info. The Requests form is a linked subform of the
Users form.
I am using the following code to find and update an existing record by
typing the UserID of an individual into the Users form. Here is how I want
it to work: If the UserID has never been entered, then I continue typing in
the form in order to create the record. If the UserID has been entered
before (i.e. a record already exists), then the pre-existing record is
displayed along with any info in the Requests subform. I can then update the
record by adding any new requests.
The code is not working. When I type in a UserID (existing or not), I get
the error "Method or Control not found." The debugger goes to the last
character of the line: RS.FindFirst "[" & C.ControlSource & "]=" & C
Any ideas?
Thanks!
Michelle
Option Explicit
Dim Found
Function Find_BeforeUpdate (F As Form)
Dim RS As Recordset, C As Control
Set C = Screen.ActiveControl
Set RS = F.RecordsetClone
On Error Goto Err_Find_BeforeUpdate
' Try to find a record with a matching value.
Select Case RS.Fields(C.ControlSource).Type
' Find using Numeric data type key value?
Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, _
DB_DOUBLE, DB_BYTE
RS.FindFirst "[" & C.ControlSource & "]=" & C
' Find using Date data type key value?
Case DB_DATE
RS.FindFirst "[" & C.ControlSource & "]=#" & C & "#"
' Find using Text data type key value?
Case DB_TEXT
RS.FindFirst "[" & C.ControlSource & "] = """ & C & """"
Case Else
MsgBox "ERROR: Invalid data type for '" & C.Name & "'!"
DoCmd.CancelEvent
Exit Function
End Select
' If a record is found, save the found record's bookmark.
If RS.NoMatch Then
Found = Null
Else
Found = RS.Bookmark
End If
' If the record is found...
' ...cancel the BeforeUpdate event
' ...undo changes made to the current record
' ...and TAB to the next control to trigger the OnExit routine.
If Not IsNull(Found) Then
DoCmd.CancelEvent
SendKeys "{ESC 2}{TAB}", False
End If
Exit Function
Err_Find_BeforeUpdate:
MsgBox "ERROR: Err " & Err & ": " & Error$, 48
DoCmd.CancelEvent
Exit Function
End Function
Function Find_OnExit ()
' If the record is found, cancel the OnExit routine to stay
' in the control and go find the record.
If Not IsNull(Found) And Len(Found) <> 0 Then
DoCmd.CancelEvent
' Synchronize the form record with the found record.
Screen.ActiveForm.Bookmark = Found
Found = Null
End If
End Function