Hey everyone,
I can't seem to figure out what's not working here. I've put everything in, and it runs but for some reason, it's not pulling the correct number. When I run the code, it returns the last entry for the Survey_ID field. However, if I add a new record to the table and re-run it. It still returns the same one as last time. Anyone have any ideas on how to get it to pull the correct ID number?
It's almost as if each time it's run, it's not clearing the recordset or requerying the db for an updated recordset.
Public Sub getSurveyID()
Dim rstTotal As New ADODB.Recordset
Set rstTotal = Nothing
Dim conTotal As New ADODB.Connection
Set conTotal = Nothing
Dim strCon As String
Dim sPath As String
Dim strTotal As String
Dim intTotal As Integer
' Open connection
sPath = ThisWorkbook.Path
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
Set conTotal = New ADODB.Connection
strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & sPath & "CARS.mdb;"
conTotal.Open strCon
Set rstTotal = New ADODB.Recordset
rstTotal.CursorLocation = adUseClient
strTotal = "Results"
rstTotal.Open strTotal, conTotal, adOpenStatic, adLockReadOnly, adCmdTable
rstTotal.MoveLast
intTotal = rstTotal.Fields("Survey_ID")
MsgBox (intTotal)
rstTotal.Close
conTotal.Close
Set rstTotal = Nothing
Set conTotal = Nothing
End Sub
Bookmarks