strambo
strambo

Reputation: 91

VBA - Convert string to datetime to compare with date in SQL

Probably a simple question, but I haven't been able to find an answer on any forums yet.

I am using VBA to push data into SQL from an open source, and what I need my code to do is check the timestamp of the most recent data in SQL to determine if it needs to retrieve more data, or it is most up to date.

By using ADODB I can retrieve the latest date from SQL as follows

print latest_date
13/07/2021 8:14:50 PM

And the date from the data I want to collect as

print resource_date
'2021-07-13 20:14:50'

As you can see, the resource_date is a string, and the latest_date is an item from an ADODB recordset. These can't be compared in an If statement to see if they're the same.

I've tried using CDate() but I get mismatched type errors.

Any help is appreciated.

Thanks

Upvotes: 0

Views: 451

Answers (1)

Gustav
Gustav

Reputation: 55806

Convert the text date to a true date value and use DateDiff to compare, for example by the second ("s"):

If DateDiff("s", print latest_date, CDate(resource_date)) > 0
    ' latest_date is earlier than resource_date.
End If

Upvotes: 1

Related Questions