textBox to Date MS Access - vba

Please have at look on my issue.
Background data: ItemB - field with date type, which has YYMMDDHHNN format
inptdate - textBox for input data in YYMMDDHHNN format
What needed, transform data from String format (inptdate) into Date format(ItemB)
My way
Private Sub Idate_AfterUpdate()
Dim mydate As String
mydate = "2212131400"
inptdate= mydate
Me.ItemB = CDate(Mid(mydate, 6, 2) & "," & Mid(mydate, 4, 2) & "," & Mid(mydate, 2, 2) & " " & Mid(mydate, 8, 2) & ":" & Mid(mydate, 10, 2))
End Sub
but my code execute with an "Type mismatch" error

You can use Format and CDate:
Dim TrueDate As Date
mydate = "2212131400"
TrueDate = CDate("20" & Format(mydate, "##\/##\/## ##\:##"))

Related

Access vba sql query with date in format dd/mm/yyyy

I searched how to solve my issue in many post but can't find an asnwer.
I need to perform a select in sql where one of the criteria is a date that is between other two dates.
My problem is that my date field in the database is a text area and i need a date to work with
If i run my sql sentence with the date writen it works, but there's a problem when i use variables, here i show the code used.
Thanks in advance
Dim fechaM As Date
Dim fechaAnt As String
Dim fechaPost As String
fechaM = Format(CDate(Nz(rs!fecha_m)), "dd/mm/yyyy")
fechaAnt = Format(CDate(Nz(rs!fecha_m)) - 7, "dd/mm/yyyy")
fechaPost = Format(CDate(Nz(rs!fecha_m)) + 7, "dd/mm/yyyy")
Set rsAguas = Db.OpenRecordset("SELECT table_user.nombre FROM table_user INNER JOIN M_A ON " & _
"table_user.localizacion = M_A.localizacion WHERE " & _
" fechaM " Between " & fechaAnt & " AND " & fechaPost & " " & _
" AND ((M_A.estado)=1)")
When i run this code i don't get error, but it doesn't retrieve data
The bast way to deal with International Dates in Access is to use Allen browne SQLDate Function Allen browne site .
the function
Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively by JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time component,
' or a date/time format if it does.
'Author: Allen Browne. allen#allenbrowne.com, June 2006.
If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If
End Function
You can use the inbuilt DateValue and DateAdd functions:
WHERE
(M_A.estado = 1) AND
DateValue(fechaM) Between DateAdd("d", -7, DateValue(fechaM)) AND DateAdd("d", 7, DateValue(fechaM))
You are mixing it a little. Try:
Dim fechaM As Date
Dim fechaAnt As String
Dim fechaPost As String
fechaM = Nz(CVDate(rs!fecha_m.Value), Date)
fechaAnt = Format(DateAdd("d", -7, fechaM), "yyyy\/mm\/dd")
fechaPost = Format(DateAdd("d", 7, fechaM), "yyyy\/mm\/dd")
Set rsAguas = Db.OpenRecordset("SELECT table_user.nombre FROM table_user INNER JOIN M_A ON " & _
"table_user.localizacion = M_A.localizacion WHERE " & _
"(fechaMfield Between #" & fechaAnt & "# AND #" & fechaPost & "#) " & _
"AND (M_A.estado = 1)")
Here, fechaMfield is the name of your date field of the table.
If fechaMfield is text, use DateValue([fechaMfield]) in the query.

Access VBA not appending date properly in loop

I'm trying to make a loop that (among other things) inserts a date and then in each row, adds a month to that date. It is not appending the first date properly. The date is a DLookup from a date field in a query, so I think it should work as a date. And I don't see anything wrong with my SQL statement. But when this runs the date shows up in the table as 12/30/1899 and if you click on it, it changes to 12:03:34 AM. It's supposed to be 5/1/15. Nothing I've tried to get this to work has given me any other results.
Here's my code, please note: there's probably a couple other things wrong with my overall code I'm sure, but I'm focusing on this date problem for now. Feel free to point out whatever you find, though.
Private Sub AmortButton_Click()
DoCmd.SetWarnings False
Dim Account As Long: Account = DLookup("[L#]", "qry_info4amort") 'working
Dim StartDate As Date: StartDate = CDate(DLookup("PaidToDate", "qry_info4amort")) 'NOT WORKING
Dim InterestRate As Double: InterestRate = DLookup("IntCur", "qry_info4amort") 'working
Dim piConstant As Integer: piConstant = DLookup("[P&IConstant]", "qry_info4amort")
Dim UPB As Currency: UPB = DLookup("UPB", "qry_info4amort") 'working
Dim tempUPB As Currency: tempUPB = UPB 'working (just to establish variable)
Dim AmortInterest As Currency: AmortInterest = 0 'working (just to establish variable)
Dim AmortPrincipal As Currency: AmortPrincipal = 0 'working (just to establish variable)
Dim Ranking As Integer: Ranking = 1 'working (just to establish variable)
Dim PaymentLoop As Integer: PaymentLoop = 1 'working (just to establish variable)
Dim PaymentNumber As Integer: PaymentNumber = DLookup("NumPmts", "qry_info4amort") 'working
Dim tempInterest As Integer: tempInterest = 0 'working (just to establish variable)
Do While PaymentLoop <= PaymentNumber 'working
tempInterest = Round(tempUPB * (InterestRate / 12), 2)
tempUPB = Round(tempUPB - (piConstant - tempInterest), 2)
AmortPrincipal = AmortPrincipal + (piConstant - tempInterest)
AmortPrincipal = (piConstant - tempInterest)
AmortInterest = AmortInterest + tempInterest
DoCmd.RunSQL "INSERT INTO tbl_AmortizationTest ([L#],[Payment#],[PaymentDate],[UPB],[Interest],[Principal],[TotalPayment],[InterestRate],[TempUPB],[Ranking]) " & _
"VALUES (" & Account & "," & PaymentLoop & "," & StartDate & "," & UPB & "," & tempInterest & "," & AmortPrincipal & "," & (tempInterest + AmortPrincipal) & "," & InterestRate & "," & tempUPB & "," & Ranking & ")"
UPB = tempUPB
StartDate = DateAdd("m", 1, StartDate) 'NOT WORKING
PaymentLoop = PaymentLoop + 1 'working
Ranking = Ranking + 1 'working
Loop
MsgBox "Done!"
DoCmd.SetWarnings True
End Sub
First, if PaidToDate is of data type Date, retrieve your date as is:
StartDate = DLookup("PaidToDate", "qry_info4amort")
If it is a string, CDate or DateValue will do:
StartDate = DateValue(DLookup("PaidToDate", "qry_info4amort"))
Second, format a proper string expression for the date when concatenating it into SQL:
... PaymentLoop & ",#" & Format(StartDate, "yyyy\/mm\/dd") & "#," & UPB ...
Instead of using CDate() around your DLookup("PaidToDate", "qry_info4amort") you are probably going to have to pull out the relevant parts piece by piece...
StartDate = DateSerial(<year>, <month>, <day>)
For example, if DLookup("PaidToDate", "qry_info4amort") returns 05012015 then you could do:
StartDate = DateSerial(Mid(DLookup("PaidToDate", "qry_info4amort"),5,4), Left(DLookup("PaidToDate", "qry_info4amort"),2), Mid(DLookup("PaidToDate", "qry_info4amort"),3,2))
If the DLookup("PaidToDate", "qry_info4amort") returns a value with / in it, then you will have to use some Instr() functions... More on that here.

DateValue("Mar/02/2015") results in type mismatch. Regional settings Germany

I wrote code to display quarters of a year instead of using exact dates.
It looks like this:
"Q" & Application.WorksheetFunction.RoundUp(Month(ws.Cells(i, 3).Value) / 3, 0) _
& " " & Year(ws.Cells(i, 3).Value)
ws.Cells(i, 3).Value can be "Mar/06/2015" and any other date. However "Feb/06/2015" works but I get a type mismatch for "Mar/06/2015". Why?
Assuming your data format is consistent, you can use this function to return an actual date value from the cell, which you can then use your Month and Year functions on:
Function GetDate(sInput As String, Optional sDelimiter As String = "/") As Date
Dim vParts
vParts = Split(sInput, sDelimiter)
GetDate = Evaluate("DateValue(""" & vParts(1) & "-" & vParts(0) & "-" & vParts(2) & """)")
End Function

Format(CDate()) result giving Wrong Results

i Have the followin code in vb
Dim sStart As String, sEnd As String
sStart = ComboBoxYear.List(ComboBoxYear.ListIndex) & "-" & ComboBoxMonth.List(ComboBoxMonth.ListIndex) & "-1"
sEnd = Format(DateAdd("M", 1, CDate(sStart)) - 1, "yyyy-MMM-dd")
Now i have changed this in VB.NET as
sStart = Me.ComboBoxYear.SelectedItem.ToString() & "-" & ComboBoxMonth.SelectedItem.ToString() & "-1"
sEnd = Format(DateAdd("M", 1, CDate(sStart)) & -1, "yyyy-MMM-dd")
But sEnd giving result as 'yyyy-MMM-dd' only, what i did wrong in my code.
Try Like This
sStart = Me.ComboBoxYear.SelectedItem.ToString() & "-" &
ComboBoxMonth.SelectedItem.ToString() & "-1"
sEnd =Format(CDate(DateAdd("M", 1, CDate(sStart)) & -1), "yyyy-MMM-dd")
Add CDate inside of DateAdd("M", 1, CDate(sStart)) & -1

VBA Macro and changing date format to mmm-yy

I am having problems with an excel macro (VBA) that is meant to grab a date from an excel spreadsheet, subtract one month and reformat it to MMM-YY. Basically I want to take 3/31/2013 and convert it to Feb-13
Here is my code:
Dim ReportDate As Date
ReportDate = Worksheets("Current").Cells(2, 16) 'ex. 03-31-2013
prevMonth = Format((Month(ReportDate) - 1) & "/" & Day(ReportDate) & "/" & Year(ReportDate), "mmm") & "-" & Format(ReportDate, "yy")
Debug.Print prevMonth
The result I get is 2/31/2013-13
So I tried changing the prevMonth variable:
prevMonth = Format((Month(ReportDate) - 1) & "/" & Day(ReportDate) & "/" & Year(ReportDate), "mmm-yy")
But got just 2/31/2013 again
I tried to declare prevMonth as an Integer or a Date but I get a type mismatch error. I can only declare it as a String but it still doesn't help the program.
Thanks in advance for your help.
Try this
Sub Demo()
Dim ReportDate As Date
Dim prevMonth As Date
ReportDate = Worksheets("Current").Cells(2, 16) 'ex. 03-31-2013
prevMonth = DateAdd("m", -1, ReportDate)
Debug.Print Format(prevMonth, "mmm-yy")
End Sub