If NotInList-Change Source

C

Clyde

I have simple cboFind that works on current data. I have an optData that
allows manual viewing of archival data. The same form then changes data
source for the forms and subforms and of course cboFind. That works fine. I
would like to automate this change. Ex: enter custnumb into cboFind >
NotInList > check archives for custnumb and if found > auto switch to
archival data and display the record.
Have tried a lot of code but keep bumping into the fact that while in
NotInList the cbo.value is Null (of course) and won't allow cbo.setfocus,
cbo.text = NewData, this after switching to archival sources for frms,
subfrms and cbo.
if not .nomatch then
optData =2
call optData_afterupdate
this reassigns source for frms, subfrms and cbo
cbofind.setfocus
cbofind.text = newdata (error cannot change text value unless has focus)
call cbofind_afterupate

Sorry to be so wordy.
 
D

dch3

The following code would be a good starting point, I've marked the areas that
you'll need to modify. The code allows the user to enter a value not in the
displayed list and advance to that record. In this implementation, the value
entered corresponds to the bound field. You can just as easily look up a
value in another field and get the key using DLookup().

Private Sub cboSelectShow_NotInList(NewData As String, Response As Integer)
'Code which selects the show based on show number is of my own doing
'Code which adds a new record curtesy of MS.
'Since the .LimitToList = True, this code allows the user to enter a job
number and locate the corresponding show from there
'Although the txtShowNumber is the first field in the combo list and the
bound field, we have to retreive the show name from
'the underlying table in order to move the list to that specific record

Dim strMsgText As String
Dim varResult As Variant
Dim rs As DAO.Recordset
Dim strShowName As String

On Error GoTo Err_cboSelectShow_NotInList

If NewData = "" Then Exit Sub

'If the value entered matches a record in the database, set the comboBox
to the
'value entered and tell Access that no error occurred. If we weren't
dealing with
'the bound value, we could obtain it using a DLookup(). For example, if
I entered
'DCH, we could use 'DCH' to lookup my employee number.
If ECount("txtShowName", "tblShowInformation", "txtShowNumber = '" &
NewData & "'") > 0 Then
Me.cboSelectShow = NewData
Response = acDataErrContinue
Me.cboSortOrder.SetFocus
Call cboSelectShow_AfterUpdate
Exit Sub
End If

'You'll probably add code here to search the alternate table. If the value
is found,
'change the control source on the comboBox, requery it to load the values,
set the
'value, and then use Response = acDataErrContinue. You'll probably want to
display
'a message that the comboBox control is now pointing to an alternate
dataSource.

strMsgText = ""
strMsgText = strMsgText & "The show number or name has not been set-up
in the database" & Chr(13) & Chr(13)
strMsgText = strMsgText & "Do you want to add the show?"

If MsgBox(strMsgText, vbInformation + vbYesNo + vbDefaultButton2) = vbNo
Then
strMsgText = "Please select a show from the list."
MsgBox strMsgText, vbInformation
Response = acDataErrContinue
Exit Sub
End If

DoCmd.Echo False
DoCmd.OpenForm "frmShowInformationDetail", , , , acFormAdd, acDialog,
NewData
DoCmd.Echo True

varResult = ECount("txtShowName", "tblShowInformation", "txtShowNumber =
'" & NewData & "'")
If IsNull(varResult) Then
' If the show was not added set the Response argument to suppress an
error message and undo changes.
strMsgText = "Please select a show from the list."
MsgBox strMsgText, vbInformation
Response = acDataErrContinue
Else
If detectDataType(NewData) = "number" Then
'Clears the value typed
Me.cboSelectShow.Undo
'Requeries to ensure the new record is present
Me.cboSelectShow.Requery
'Sets the value
Me.cboSelectShow = NewData
'Clears the error
Response = acDataErrContinue
'Closes the drop down list if viewed
Me.cboSortOrder.SetFocus
End If
If detectDataType(NewData) = "name" Then
Response = acDataErrAdded
'Shift the focus for cosmetic reasons to give the same effect
when the show number is entered
Me.cboSortOrder.SetFocus
End If
Call cboSelectShow_AfterUpdate
End If

Exit_cboSelectShow_NotInList:
Exit Sub

Err_cboSelectShow_NotInList:
MsgBox getDefaultErrorMessage(Me.Name, "cboSelectShow_NotInList",
Err.Number, AccessError(Err.Number)), vbCritical
Resume Exit_cboSelectShow_NotInList

End Sub
 
C

Clyde

Thanks a lot. Was able to sort it out from your example. My app would never
add a new customer through the cbo so all I needed was the "requery" hint to
enable the cbo new recordsource. Thanks again.
 
D

dch3

I guarantee that at some point you'll find the ability to add a record via
the .NotInList event usefull. We all do...you cannot escape it...it
is...destiny...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top