21erinswift
asked on
Fill List Box from Combo Box selection
I have a form where once the user selects an option from a combo box, two list boxes should fill based on queries. My VBA code is below. I'm currently having two problems with my form. First of all, I get an error number 424 (object required) in my form_activate sub on the line indicated in the below code. Secondly, the textbox that is suppose to be grabbing the value of the first column has a value of "#Name?" instead. I know this is an error of some kind, but I'm not sure where to fix it.
Option Compare Database
'************************* ********** ********** ********** **
'
'Form Name: frmAssign_Course
'
'************************* ********** ********** ********** *****
Dim blnSelectRecord As Boolean 'Record is selected
Private Function LockContactInfo(IsLocked As Boolean)
On Error GoTo errLock
'on false, disable list boxes and buttons
Me!lstNotInCourse.Enabled = Not IsLocked
Me!lstInCourse.Enabled = Not IsLocked
Me!cmdAddUsers.Enabled = Not IsLocked
Me!cmdRemoveUsers.Enabled = Not IsLocked
Me!cmdSubmit.Enabled = Not IsLocked
errLock:
MsgBox (Err.Description & "Lock")
End Function
Public Sub Refresh_InCourseData()
On Error GoTo errRefreshIn
'declare variables needed to set up connection and update lstInCourse
Dim connRefresh As ADODB.Connection
Dim rsRefresh As ADODB.Recordset
Dim strStatement As String
Dim strRowList As String
'set up connection
Set connRefresh = CurrentProject.Connection
'SQL statement to fill lstInCourse
strStatement = "SELECT USER.USERNAME, USER.CUSTNMBR " & _
"FROM USERS INNER JOIN (COURSES INNER JOIN tblLINK " & _
"ON COURSES.COURSEID = tblLINK.COURSEID) " & _
"ON USERS.CUSTNMBR = tblLINK.CUSTNMBR " & _
"WHERE ((tblLINK.COURSEID)= & [Forms]![frmAssign_Course] ![txtCours eID])" & _
"ORDER BY USER.USERNAME"
'set recordset
Set rsRefresh = New ADODB.Recordset
rsRefresh.Open strStatement, connRefresh
'clear lstInCourse
strRowList = ""
lstInCourse.RowSource = strRowList
'set up column formatting
Me!lstInCourse.ColumnCount = 2
'set header
strRowList = "User Name, Customer Num;"
Me!lstInCourse.RowSource = strRowList
'populate lstInCourse
If (Not rsRefresh.BOF) Then 'if not before the first record
While (Not rsRefresh.EOF) 'while not the end of recordset
strRowList = strRowList & rsRefresh.Fields("USERNAME ").Value & " -- " & rsRefresh.Fields("CUSTNMBR ").Value
rsRefresh.MoveNext
Wend
End If
'assign string to list box
Me!lstInCourse.RowSource = strRowList
'close recordset and connection
rsRefresh.Close
Set connRefresh = Nothing
errRefreshIn:
MsgBox (Err.Description & "Refresh In")
End Sub
Public Sub Refresh_NotInCourseData()
On Error GoTo errRefreshNot
'declare variables needed to set up connection and update lstInCourse
Dim connRefresh As ADODB.Connection
Dim rsRefresh As ADODB.Recordset
Dim strStatement As String
Dim strRowList As String
'set up connection
Set connRefresh = CurrentProject.Connection
'SQL statement to fill lstInCourse
strStatement = "SELECT USER.USERNAME, USER.CUSTNMBR " & _
"FROM USERS " & _
"WHERE (((USERS.CUSTNMBR) NOT IN (" & _
"SELECT USERS.CUSTNMBER FROM USER " & _
"INNER JOIN (COURSES INNER JOIN tblLINK " & _
"ON COURSES.COURSEID = tblLINK.COURSEID) " & _
"ON USERS.CUSTNMBR = tblLINK.CUSTNMBR " & _
"WHERE ((tblLINK.COURSEID)= & [Forms]![frmAssign_Course] ![cmboCour se.Column( 0).value]) " & _
"ORDER BY USER.USERNAME"
'set recordset
Set rsRefresh = New ADODB.Recordset
rsRefresh.Open strStatement, connRefresh
'clear lstInCourse
strRowList = ""
lstNotInCourse.RowSource = strRowList
'set up column formatting
Me!lstNotInCourse.ColumnCo unt = 2
'set header
strRowList = "User Name, Customer Num;"
Me!lstNotInCourse.RowSourc e = strRowList
'populate lstInCourse
If (Not rsRefresh.BOF) Then 'if not before the first record
While (Not rsRefresh.EOF) 'while not the end of recordset
strRowList = strRowList & rsRefresh.Fields("USERNAME ").Value & " -- " & rsRefresh.Fields("CUSTNMBR ").Value
rsRefresh.MoveNext
Wend
End If
'assign string to list box
Me!lstNotInCourse.RowSourc e = strRowList
'close recordset and connection
rsRefresh.Close
Set connRefresh = Nothing
errRefreshNot:
MsgBox (Err.Description & "Refresh Not")
End Sub
Private Sub Form_Activate()
'when form is activated, check for correct data to display
On Error GoTo errActivate
'If a Course is selected in combo box
If Not IsNull(Me![cmboCourse]) Or Me![cmboCourse] = "" Then
'make sure appropriate records are displayed
blnSelectRecord = True
'<<<<<<<<<<ERROR IS ON NEXT LINE>>>>>>>>>>>
Me.Recordset.FindFirst "[COURSEID]=" & Me![cmboCourse].Column(0). value
'refresh list boxes
Refresh_InCourseData
Refresh_NotInCourseData
End If
errActivate:
MsgBox (Err.Description & Err.Number & " - Activate")
End Sub
Private Sub cmboCourse_AfterUpdate()
'After selecting an item in the combo box
On Error GoTo errCUpdate
Dim rs As Object
Dim strCouseID As String
Me!txtCourseID.Requery
If Not (IsNull(Me![cmboCourse]) Or Me![cmboCourse] = "") Then
Set rs = Me.RecordsetClone
rs.FindFirst "[CourseID] = " & Str(Me![cmboCourse])
'set recordset to selected item in combo box
Me.Bookmark = rs.Bookmark
End If
Me.Refresh
'Show error description on error
errCUpdate:
MsgBox (Err.Description & " Update")
End Sub
Private Sub cmboCourse_Change()
'On changing the combo box
On Error GoTo errCChange
strCourseID = "SELECT COURSEID FROM COURSE " & _
"WHERE COURSENAME = [Forms]![frmAssign_Course] ![cmboCour se.column( 1).value]"
txtCourseID.Value = strCourseID
'If a record is selected, lock the list boxes
blnSelectRecord = True
'Get recordset
Dim rs As Object
Dim blnMatch As Boolean
'set recordset
Set rs = Me.Recordset
blnMatch = False 'haven't found the record
rs.MoveFirst 'move to first record
While Not blnMatch
'Browse through the recordset until correct record is found
If rs!COURSEID = Val(Me![txtCourseID]) Then
blnMatch = True
Else
rs.MoveNext
End If
Wend
'Once match is found, refresh list boxes
Refresh_InCourseData
Refresh_NotInCourseData
'enable buttons and boxes
Me!cmdAddUsers.Enabled = True
Me!cmdRemoveUsers.Enabled = True
Me!lstNotInCourse.Enabled = True
Me!lstInCourse.Enabled = True
'On error, show error description
errCChange:
MsgBox (Err.Description & " Change")
End Sub
Private Sub cmdSubmit_Click()
'Submits changes to database and locks fields
On Error GoTo errSubmit
If Not blnSelectRecord Then
'if a record is not selected ask to select record
MsgBox ("Please Select a Course")
Else
'if a record is selected, lock the fields
'(users are saved to course before submit)
LockContactInfo True
End If
'save results
DoCmd.Save
'on error, show error description and exit sub
errSubmit:
MsgBox (Err.Description & "Submit")
Exit Sub
End Sub
Private Sub cmdClose_Click()
On Error GoTo errClose
DoCmd.Close
errClose:
MsgBox (Err.Description & "Close")
End Sub
Option Compare Database
'*************************
'
'Form Name: frmAssign_Course
'
'*************************
Dim blnSelectRecord As Boolean 'Record is selected
Private Function LockContactInfo(IsLocked As Boolean)
On Error GoTo errLock
'on false, disable list boxes and buttons
Me!lstNotInCourse.Enabled = Not IsLocked
Me!lstInCourse.Enabled = Not IsLocked
Me!cmdAddUsers.Enabled = Not IsLocked
Me!cmdRemoveUsers.Enabled = Not IsLocked
Me!cmdSubmit.Enabled = Not IsLocked
errLock:
MsgBox (Err.Description & "Lock")
End Function
Public Sub Refresh_InCourseData()
On Error GoTo errRefreshIn
'declare variables needed to set up connection and update lstInCourse
Dim connRefresh As ADODB.Connection
Dim rsRefresh As ADODB.Recordset
Dim strStatement As String
Dim strRowList As String
'set up connection
Set connRefresh = CurrentProject.Connection
'SQL statement to fill lstInCourse
strStatement = "SELECT USER.USERNAME, USER.CUSTNMBR " & _
"FROM USERS INNER JOIN (COURSES INNER JOIN tblLINK " & _
"ON COURSES.COURSEID = tblLINK.COURSEID) " & _
"ON USERS.CUSTNMBR = tblLINK.CUSTNMBR " & _
"WHERE ((tblLINK.COURSEID)= & [Forms]![frmAssign_Course]
"ORDER BY USER.USERNAME"
'set recordset
Set rsRefresh = New ADODB.Recordset
rsRefresh.Open strStatement, connRefresh
'clear lstInCourse
strRowList = ""
lstInCourse.RowSource = strRowList
'set up column formatting
Me!lstInCourse.ColumnCount
'set header
strRowList = "User Name, Customer Num;"
Me!lstInCourse.RowSource = strRowList
'populate lstInCourse
If (Not rsRefresh.BOF) Then 'if not before the first record
While (Not rsRefresh.EOF) 'while not the end of recordset
strRowList = strRowList & rsRefresh.Fields("USERNAME
rsRefresh.MoveNext
Wend
End If
'assign string to list box
Me!lstInCourse.RowSource = strRowList
'close recordset and connection
rsRefresh.Close
Set connRefresh = Nothing
errRefreshIn:
MsgBox (Err.Description & "Refresh In")
End Sub
Public Sub Refresh_NotInCourseData()
On Error GoTo errRefreshNot
'declare variables needed to set up connection and update lstInCourse
Dim connRefresh As ADODB.Connection
Dim rsRefresh As ADODB.Recordset
Dim strStatement As String
Dim strRowList As String
'set up connection
Set connRefresh = CurrentProject.Connection
'SQL statement to fill lstInCourse
strStatement = "SELECT USER.USERNAME, USER.CUSTNMBR " & _
"FROM USERS " & _
"WHERE (((USERS.CUSTNMBR) NOT IN (" & _
"SELECT USERS.CUSTNMBER FROM USER " & _
"INNER JOIN (COURSES INNER JOIN tblLINK " & _
"ON COURSES.COURSEID = tblLINK.COURSEID) " & _
"ON USERS.CUSTNMBR = tblLINK.CUSTNMBR " & _
"WHERE ((tblLINK.COURSEID)= & [Forms]![frmAssign_Course]
"ORDER BY USER.USERNAME"
'set recordset
Set rsRefresh = New ADODB.Recordset
rsRefresh.Open strStatement, connRefresh
'clear lstInCourse
strRowList = ""
lstNotInCourse.RowSource = strRowList
'set up column formatting
Me!lstNotInCourse.ColumnCo
'set header
strRowList = "User Name, Customer Num;"
Me!lstNotInCourse.RowSourc
'populate lstInCourse
If (Not rsRefresh.BOF) Then 'if not before the first record
While (Not rsRefresh.EOF) 'while not the end of recordset
strRowList = strRowList & rsRefresh.Fields("USERNAME
rsRefresh.MoveNext
Wend
End If
'assign string to list box
Me!lstNotInCourse.RowSourc
'close recordset and connection
rsRefresh.Close
Set connRefresh = Nothing
errRefreshNot:
MsgBox (Err.Description & "Refresh Not")
End Sub
Private Sub Form_Activate()
'when form is activated, check for correct data to display
On Error GoTo errActivate
'If a Course is selected in combo box
If Not IsNull(Me![cmboCourse]) Or Me![cmboCourse] = "" Then
'make sure appropriate records are displayed
blnSelectRecord = True
'<<<<<<<<<<ERROR IS ON NEXT LINE>>>>>>>>>>>
Me.Recordset.FindFirst "[COURSEID]=" & Me![cmboCourse].Column(0).
'refresh list boxes
Refresh_InCourseData
Refresh_NotInCourseData
End If
errActivate:
MsgBox (Err.Description & Err.Number & " - Activate")
End Sub
Private Sub cmboCourse_AfterUpdate()
'After selecting an item in the combo box
On Error GoTo errCUpdate
Dim rs As Object
Dim strCouseID As String
Me!txtCourseID.Requery
If Not (IsNull(Me![cmboCourse]) Or Me![cmboCourse] = "") Then
Set rs = Me.RecordsetClone
rs.FindFirst "[CourseID] = " & Str(Me![cmboCourse])
'set recordset to selected item in combo box
Me.Bookmark = rs.Bookmark
End If
Me.Refresh
'Show error description on error
errCUpdate:
MsgBox (Err.Description & " Update")
End Sub
Private Sub cmboCourse_Change()
'On changing the combo box
On Error GoTo errCChange
strCourseID = "SELECT COURSEID FROM COURSE " & _
"WHERE COURSENAME = [Forms]![frmAssign_Course]
txtCourseID.Value = strCourseID
'If a record is selected, lock the list boxes
blnSelectRecord = True
'Get recordset
Dim rs As Object
Dim blnMatch As Boolean
'set recordset
Set rs = Me.Recordset
blnMatch = False 'haven't found the record
rs.MoveFirst 'move to first record
While Not blnMatch
'Browse through the recordset until correct record is found
If rs!COURSEID = Val(Me![txtCourseID]) Then
blnMatch = True
Else
rs.MoveNext
End If
Wend
'Once match is found, refresh list boxes
Refresh_InCourseData
Refresh_NotInCourseData
'enable buttons and boxes
Me!cmdAddUsers.Enabled = True
Me!cmdRemoveUsers.Enabled = True
Me!lstNotInCourse.Enabled = True
Me!lstInCourse.Enabled = True
'On error, show error description
errCChange:
MsgBox (Err.Description & " Change")
End Sub
Private Sub cmdSubmit_Click()
'Submits changes to database and locks fields
On Error GoTo errSubmit
If Not blnSelectRecord Then
'if a record is not selected ask to select record
MsgBox ("Please Select a Course")
Else
'if a record is selected, lock the fields
'(users are saved to course before submit)
LockContactInfo True
End If
'save results
DoCmd.Save
'on error, show error description and exit sub
errSubmit:
MsgBox (Err.Description & "Submit")
Exit Sub
End Sub
Private Sub cmdClose_Click()
On Error GoTo errClose
DoCmd.Close
errClose:
MsgBox (Err.Description & "Close")
End Sub
ASKER
Okay, I did and I still get the same error
Remove the .Value property, i.e.:
Me.Recordset.FindFirst "[COURSEID]=" & Me![cmboCourse].Column(0)
Since you are explicitly referring to a combo box's column, the Value property does not apply to a Column.
Me.Recordset.FindFirst "[COURSEID]=" & Me![cmboCourse].Column(0)
Since you are explicitly referring to a combo box's column, the Value property does not apply to a Column.
The same applies to the textbox that is trying to retrieve the value.
If you have:
=Form!cmboCourse.Column(1) .Value
use:
=Form!cmboCourse.Column(1)
Note that you cannot use Me! in a control source. If you have it, replace it with Form!
If you have:
=Form!cmboCourse.Column(1)
use:
=Form!cmboCourse.Column(1)
Note that you cannot use Me! in a control source. If you have it, replace it with Form!
ASKER
Now I get error 91 - object variable or with block variable not set
Try changing the line to read:
Me.RecordsetClone.FindFirs t "[COURSEID]=" & Me![cmboCourse].Column(0)
Me.Bookmark=Me.RecordsetCl one.Bookma rk
Does that work?
Me.RecordsetClone.FindFirs
Me.Bookmark=Me.RecordsetCl
Does that work?
ASKER
No, now I get an error that there is an invalid reference to RecordsetClone property. Me.RecordsetClone.FindFirs t is not valid
Hmm, is your form bound to an ADO recordset?
Check placement of quotes in your sql statements.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
shanesuebsahakarn - Adding that line got rid of my errors, but my text box still isn't displaying the correct answer. I'm going to close this question and open a new one for that. Thank you for your help.
Erin
Erin
ASKER
Sorry, my fault, this didn't actually fix the problem. I just didn't re activate the form.
What's the recordset of your form? Is it bound to an SQL Server table, or an Access table?
Option Compare Database
Option Explicit
'*************************