Home > database >  Setting Combobox Value With VBA MS Access
Setting Combobox Value With VBA MS Access

Time:10-03

I am trying to make a module to pass a single result of a query into a Combobox to be populated/displayed immediately on Form_Load event, but I keep getting the following error: Run-time error '2465' "Microsoft Access can't find the field 'MyCombo' referred to in your expression" Query result is tested and returning the proper value. the problem is in the reference call to the MyCombo combobox.

This is my code below:

Public Function getSetRefNo() As String
Dim rs1 As DAO.Recordset
    
Dim currentformName As String
currentformName = Screen.ActiveForm.Name
Dim docidrefnoquery As String
Dim dociddefaultvalue As String
      
Set rs1 = CurrentDb.OpenRecordset("SELECT DISTINCT ColA FROM TblA WHERE ColC = " & Forms(currentformName)![Combo25])
    
Do Until rs1.EOF = True
    docidrefnoquery = rs1(0)
    rs1.MoveNext
Loop
rs1.Close
Set rs1 = Nothing

dociddefaultvalue = DLookup("RefNo", docidrefnoquery) 'RefNo here is the target column in the Query

Forms(currentformName)![MyCombo] = dociddefaultvalue 'MyCombo here is the Combobox Name
Debug.Print docidrefnoquery & " - " & dociddefaultvalue
End Function

on the targeted form, I use this code:

Private Sub Form_Load()
    Call getSetRefNo
End Sub

after opening the targeted form, I receive the above mentioned error. I don't know what's wrong I tried to trace everything and it seems to be fine, I used the same chunk of codes in other places and worked fine. don't know what's wrong here to be honest. I would be grateful if you could help me elaborate what's going on.

CodePudding user response:

I had to alter the form_load event like the following:

Private Sub Form_Load()
Me.TimerInterval = 30
End Sub

Private Sub Form_Timer()
Me.TimerInterval = 0
Call getSetRefNo
End Sub
  • Related