My problem is that sometimes the listbox (lstCountries) below updates/requeries after either the add_Click() or delete_Click() functions, and sometimes not. The data in tblCountries does change everytime, but the contents of the listbox don't always. I can't find any rhyme or reason to it. Can anyone tell from my code what I am doing wrong? Thanks ahead of time.
Private Sub add_Click()
On Error GoTo Err_add_Click
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "tblCountries", getConn(), adOpenDynamic, adLockOptimistic
With rs
.AddNew
![chrName] = Me.txtNewCountry
.Update
End With
rs.Close
Set rs = Nothing
Exit_add_Click:
Me.lstCountries.Requery
Refresh
Exit Sub
Err_add_Click:
MsgBox Err.Description
Resume Exit_add_Click
End Sub
Private Sub delete_Click()
On Error GoTo Err_delete_Click
Dim rs As ADODB.Recordset
Dim strSql As String
Dim country As Variant
Dim selCount As Integer
Set rs = New ADODB.Recordset
strSql = "SELECT * FROM tblCountries WHERE "
selCount = 0
For Each country In Me.lstCountries.ItemsSelected
If Not selCount = 0 Then
strSql = strSql & " AND "
End If
strSql = strSql & "idsCountryID=" & Me.lstCountries.ItemData(country)
selCount = selCount + 1
Next
If selCount > 0 Then
rs.Open strSql, getConn(), adOpenDynamic, adLockOptimistic
rs.MoveFirst
While Not rs.EOF
rs.delete
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
End If
Exit_delete_Click:
Me.lstCountries.Requery
Refresh
Exit Sub
Err_delete_Click:
MsgBox Err.Description
Resume Exit_delete_Click
End Sub
Private Sub add_Click()
On Error GoTo Err_add_Click
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "tblCountries", getConn(), adOpenDynamic, adLockOptimistic
With rs
.AddNew
![chrName] = Me.txtNewCountry
.Update
End With
rs.Close
Set rs = Nothing
Exit_add_Click:
Me.lstCountries.Requery
Refresh
Exit Sub
Err_add_Click:
MsgBox Err.Description
Resume Exit_add_Click
End Sub
Private Sub delete_Click()
On Error GoTo Err_delete_Click
Dim rs As ADODB.Recordset
Dim strSql As String
Dim country As Variant
Dim selCount As Integer
Set rs = New ADODB.Recordset
strSql = "SELECT * FROM tblCountries WHERE "
selCount = 0
For Each country In Me.lstCountries.ItemsSelected
If Not selCount = 0 Then
strSql = strSql & " AND "
End If
strSql = strSql & "idsCountryID=" & Me.lstCountries.ItemData(country)
selCount = selCount + 1
Next
If selCount > 0 Then
rs.Open strSql, getConn(), adOpenDynamic, adLockOptimistic
rs.MoveFirst
While Not rs.EOF
rs.delete
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
End If
Exit_delete_Click:
Me.lstCountries.Requery
Refresh
Exit Sub
Err_delete_Click:
MsgBox Err.Description
Resume Exit_delete_Click
End Sub