Manny
Manny

Reputation: 129

Query Date From a Table in Access VBA

I've looked around everywhere but I've found any solutions. I'm trying to query a date from a table in Access VBA but after trying various placements of "#", I still can't get it to work. Any help would be greatly appreciated.

Code:

Dim varStartDate As Date

varStartDate = "SELECT Employees.StartDate FROM Employees WHERE Employees.Name LIKE Manny"

I get type mismatch error.

I also tried

varStartDate = "# " & "SELECT Employees.StartDate FROM Employees WHERE Employees.Name LIKE Manny" & " #"

Same error.

Upvotes: 2

Views: 181

Answers (1)

HansUp
HansUp

Reputation: 97101

The type mismatch error was because you were attempting to assign a string to varStartDate, but since it was declared As Date, it can't accept "SELECT Employees.StartDate FROM ...".

I think you can get what you need with DLookup.

Dim varStartDate As Variant
varStartDate = DLookup("StartDate", "Employees", "[Name]='Manny'")

If another issue is that Employees.StartDate is text instead of Date/Time datatype, maybe you also need to convert the value you get from DLookup.

If Not IsNull(varStartDate) Then
    varStartDate = CDate(varStartDate)
End If

Upvotes: 1

Related Questions