VBA DLOOKUP() Date/Time but only compare on Time - vba

I have this script to get the data less than or equal the time 11:59:00 AM:
Me.lbl001.Caption = Format(DLookup("DateTime", "tblTime", "[ID]=" & Me.txtMacID & " AND [DateTime]<=#5/2/2017 11:59:00 AM#), "hh:mm:ss")
But it affects the Date so I'm also getting the data of 5/1/2017 and less. I'm trying to achieve getting data less than 11:59:00 AM within 5/2/2017 only.

Try this:
Format(DMax("DateTime", "tblTime", "[ID]=" & Me.txtMacID & " AND DateValue([DateTime])=#2/5/2017#"), "hh:mm:ss")

Related

VBA for Excel misbehaving... doesn't return the right value

I have the following function in my worksheet...
Format(Range("AG1"), "mm")
I'm using it to auto-populate the saveas filename, which is a larger function, but it all works except for this part. As part of that larger function there is
Year(Range("AG1"))
which is returning the correct year in "yyyy" format. (It doesn't require any specific formatting, it just returns the year in 4-digit format.)
However, the first function, Format(Range("AG1"), "mm") is always returning 01, indicating that is is January, which it isn't always. I need the month based on the current date to be returned in "mm" format.
The Range("AG1") refers to a cell in the worksheet that has a date entered as [mm/dd/yyyy].
Any ideas what I'm doing wrong?
According to this table you should be using "m"
No. Formatting code Display as Example
1 ddd Weekday name Sat
2 dddd Weekday name Saturday
3 mmm Month name Jan
4 mmmm Month name January
5 m Month 1
6 d Day 17
Here's the final code that worked for me...
Private Sub btnCreate_Click()
ActiveSheet.Copy
Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",False)"
ActiveSheet.Shapes.Range(Array("btnCreate")).Select
Selection.Delete
ActiveWorkbook.SaveAs "C:\Users\" & (Environ$("Username")) & "\Desktop\" & "Timecard, " & Year(Range("AG1")) & "-" & Format(Range("AG1"), "mm") & " - " & _
Range("R2").Value & ", " & Range("T2").Value, xlOpenXMLWorkbookMacroEnabled
ActiveWindow.Close
End Sub

Remove time from datetimepicker

I tried doing
senddate.Value = Format(senddate.Value, "mm/dd/yyyy")
sstartdate.Value = Format(sstartdate.Value, "mm/dd/yyyy")
MsgBox(senddate.Value & " " & sstartdate.Value)
but the messagebox still returns the time as well. (message box is there for diagnostic purposes, though it also passes the time to the SQL string as well.)
I tried switching the date time pickers to custom format and it didn't work. Any ideas?
I need to turn it into a short, simple date so that I can input it into an SQL Access BETWEEN query
senddate.Value = Format(senddate.Value, "MM/dd/yyyy")
sstartdate.Value = Format(sstartdate.Value, "MM/dd/yyyy")
MsgBox(senddate.Value & " " & sstartdate.Value).ToString
the month part should be in capital lettrs.

Excel 2013 VBA , hour check inside a range IF statement not working properly

I'm working on a bit of code for my Campus Food Shelf. I'm currently trying to prevent users from manually entering the time for normal transactions (mostly because there's a training issue and no one's doing it uniformly). However, I need the time option within this user form, in the case of data loss, so that everything can be reentered quickly.
So with the following code, I'm trying to check if the user is entering a time that's within an hour of the current time. If not, the user form should use the user's input. However, if it is within an hour then it should just use the current time, with the correct format. It runs properly when TextBox2 is empty. However, when I put 1:58:33 at 2:04 PM it still posts 1:58:33 in column E. Have I done the math wrong? I just want it to post the current time when the user tries entering a time that's within an hour.
If TextBox2.Value <> "" Then
timeCheck1 = Hour(Time) - 1
timeCheck2 = Hour(Time)
timeValue = Hour(TextBox2.Value)
If timeValue < 7 Then
timeValue = timeValue + 12
End If
If timeCheck1 < timeValue And timeValue <> timeCheck2 Then
Worksheets(Sheets.Count).Range("E" & currentRows).Value = TextBox2.Value & " " & ComboBox8.Value
Else
Worksheets(Sheets.Count).Range("E" & currentRows).Value = Format(Time, "h:mm AM/PM")
End If
Else
Worksheets(Sheets.Count).Range("E" & currentRows).Value = Format(Time, "h:mm AM/PM")
End If
Thank you all for your help in advance!
Thanks hpf! That worked. I made it a sub since I use it three times in the code. I should probably work on creating several other subs for these large bulky programs I've built...
Private Sub TimeCheck(currentRows As Integer)
If DateDiff("h", TextBox2.Value, Time()) > 1 Then
Worksheets(Sheets.Count).Range("E" & currentRows).Value = TextBox2.Value & " " & ComboBox8.Value
Else
Worksheets(Sheets.Count).Range("E" & currentRows).Value = Format(Time, "h:mm AM/PM")
End If
End Sub

How to add leading zero to time using VB.NET?

In the code below, I'm using the current date and time, with the help of which I am generating a file name. My problem is that it's giving me an output without leading zeros:
Dim strDateTime As String = DateTime.Now.Day.ToString() & "" & _
DateTime.Now.Month.ToString() & "" & DateTime.Now.Year.ToString() & "" & _
DateTime.Now.Hour.ToString() & "" & DateTime.Now.Minute.ToString() & "" & _
DateTime.Now.Second.ToString() & DateTime.Now.Millisecond.ToString()
For example, my query is giving output as below currently:
Assume time is 1:5:30 :: hh:mm:ss
Required output is: 01:05:30
How can I achieve this?
Try this,
DateTime.Now.Hour.ToString("00") & ":" & DateTime.Now.Minute.ToString("00") & ":" & DateTime.Now.Second.ToString("00")
EDIT :
As suggested by 'mdb' in the answers, using Custom Date and Time Format Strings would be more efficient and cleaner
DateTime.Now.ToString("hh:mm:ss tt") '12 Hour format with AM/PM designator, Eg :- 09:01:01 PM
DateTime.Now.ToString("HH:mm:ss") '24 Hour format Eg :- 21:01:01
Instead of concatenating strings, which is highly inefficient, you'll want to use String.Format. This also supports Custom Date and Time Format Strings, which make it trivial to achieve what you want.

VB.net - CDATE rsData

I'm doing the following to try and determine whether display a course online based on whether its in the future or would have started in the last 30 days.
If Date.Today.AddDays(-30) >= CDate(rsData("Start")) then
html += "<tr><td>" & rsData("Start") & "</td><td>" & rsData("WEEKS") & "</td><td>" &
rsData("DAYSTIME") & "</td>
However this doesn't seem to work and blocks for instance the following rsDATA(("Start")) value.
2012-09-10 00:00:00.0000000
Any ideas?
The logic is backwards, try this:
If Date.Today.AddDays(-30) <= CDate(rsData("Start")) Then
'in last 30 days, or the future
End If