Hi Community,
Hoping you can assist with helping me understand what i'm doing wrong with code I found from another post for querying an Microsoft Access Database to retrieve records WHERE a Column value = GetUserName().
When i run the code I either get an error, OR i don't get an error and it pulls the column name instead of the actual rows / records... Not sure what i'm doing wrong..
Using this specifc codie i get the following error message:
"Undefined function 'GetUserName' in expression. Error at line :0, Error Number :-2147217900"
Hoping you can assist with helping me understand what i'm doing wrong with code I found from another post for querying an Microsoft Access Database to retrieve records WHERE a Column value = GetUserName().
When i run the code I either get an error, OR i don't get an error and it pulls the column name instead of the actual rows / records... Not sure what i'm doing wrong..
Using this specifc codie i get the following error message:
"Undefined function 'GetUserName' in expression. Error at line :0, Error Number :-2147217900"
Code:
Public Sub ShiftSwap_DBOpen() Dim cn As Object, rs As Object, rs1 As Object
Dim intColIndex As Integer
Dim DBFullName As String
Dim TargetRange As Range
DBFullName = "C:\Users\MyName\Documents\ShiftSwapDB.mdb"
On Error GoTo Whoa
Application.ScreenUpdating = False
Set TargetRange = Sheets("Sheet2").Range("A5")
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName & ";"
Set rs = CreateObject("ADODB.Recordset")
rs.Open "SELECT Req_Key, Submitted_Date, Swap_Req_Date, Swap_Req_Shift, Swap_Day_Work, Swap_Req_Time FROM ShiftSwap [COLOR=#FF0000]WHERE Req_Key[/COLOR] = [COLOR=#FF0000]GetUserName()[/COLOR]", cn, , , adCmdText
' Write the field names
For intColIndex = 0 To rs.Fields.Count - 1
TargetRange.Offset(1, intColIndex).Value = rs.Fields(intColIndex).Name
Next
' Write recordset
TargetRange.Offset(1, 0).CopyFromRecordset rs
LetsContinue:
Application.ScreenUpdating = True
On Error Resume Next
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
On Error GoTo 0
Exit Sub
Whoa:
MsgBox "Error Description :" & Err.Description & vbCrLf & _
"Error at line :" & Erl & vbCrLf & _
"Error Number :" & Err.Number
Resume LetsContinue
End Sub