In the below code I am trying to obtain a specific field value but everytime the query runs in the code it produces an empty string.
Private Function GetNo() As String
On Error Resume Next
Dim number As String
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim connectString As String
Dim databasePath As String
Dim sql As String
Dim Day As Date
Dim message As String
Dim pDate As String
Day = Date()
pDate = Format$(Day, "mm/dd/yyyy")
'initialize the database connection
databasePath = ResolveAlias("database")
connectString = "Provider=Microsoft.Jet.OL
EDB.4.0;Da
ta Source=" & databasePath
sql = "SELECT TOP 1 * FROM FS_BankCashSet WHERE CashSetCreatedDate = '" & pDate & "' ORDER BY CashSetNumber ASC"
message = "SQL: " & sql
LogError "GetCashSetNo", message
If (databasePath <> Empty) Then
conn.Open connectString
If (conn.State = adStateOpen And Err.Number = 0) Then
rs.Open sql, conn, adOpenDynamic, adLockOptimistic
If (rs.State = adStateOpen And Err.Number = 0) Then
number = Left(rs.Fields("CashSetNum
berString"
).Value & "", 4)
End If
Else
message = "Couldn't query to obtain cash set number." & vbCrLf _
& "SQL: " & sql & vbCrLf _
& "Error Number: " & Err.Number & vbCrLf _
& "Error Description: " & Err.Description
LogError "GetNo", message
End If
Else
message = "Couldn't connect to the database at '" & databasePath & "'." & vbCrLf _
& "Error Number: " & Err.Number & vbCrLf _
& "Error Description: " & Err.Description
LogError "GetNo", message
End If
If (rs.State = adStateOpen) Then rs.Close
Set rs = Nothing
If (conn.State = adStateOpen) Then conn.Close
Set conn = Nothing
GetNo = number
Err.Clear
End Function
Start Free Trial