How to create a date using strings in vba? - ms-access

I want to run a for-loop from sometime to some specific time. Let's say from the first day of the year to the last day:
I am given the year and I need to add the month and the day to it:
I am trying to concatenate into a full date string and the compiler keeps throwing errors:
dim dt as Date
dim rs as recordset
set rs = currentdb.openRecordset("select max(date) as dan from Atbl")
for dt = #1/1/year(now)# to #2/2/Year(rs!dan)#
msgbox dt
Next
any help is appreciated. or any hint is welcome

DateSerial should make this easier.
Give it values for year, month, and day in that order, and it will give you back the corresponding date as a Date/Time value.
for dt = DateSerial(Year(Date), 1, 1) to DateSerial(rs!dan, 2, 2)
msgbox dt
Next

Nevermind, I found a solution quite fast: This works fine
dim dt as Date
dim rs as recordset
set rs = currentdb.openRecordset("select max(date) as dan from Atbl")
'save the first day as string first
Dim firstDay As String
firstDay = "1/1/" & Year(Now)
'convert to date
Dim firstDate As Date
firstDate = CDate(firstDay)
'save as string
Dim lastDay as string
lastDay = "2/2/" & Year(rs!dan)
'convert to date
Dim lastDate As Date
lastDate = CDate(lastDay)
'works fine in here
for dt = firstDate to lastDate
msgbox dt
next

Related

Remove user inputbox and replace with yesterday's date (automate process)

I have the following code and have been asked to remove the user input box and automate pulling in files. One issue is if the current day is Monday, we would need Friday's date pulled in (not yesterday). I am sorry for being extremely basic, but I believe I would need to replace InputBox("Input Date") with something like DateAdd(Day, DateDiff(Day, 1, GETDATE()), 0) - but it does not like Day or DD.
Any ideas? (and thank you for being patient with me in advance)
DoCmd.SetWarnings False
Dim InvDateStr As String
Dim InvDate As Date
Dim Directory_Sheets As String
Dim filename_sheets As String
Dim db As Database
Dim rst As DAO.Recordset
Dim startdate As Date
Dim EndDate As Date
'startdate = #9/23/2010#
'enddate = #1/12/2011#
'InvDate = startdate
'Do Until InvDate = enddate
InvDateStr = InputBox("Input Date")
If InvDateStr = "" Then
Exit Sub
End If
InvDate = CDate(InvDateStr)
Directory_Sheets = FilePath & "Inventory_Surveys\"
On Error Resume Next
' Pull Stores with Inventory
I'm only going to do the date part, you can implement it:
InvDate = Date - 1 'Yesterday
Do While Weekday(InvDate, vbMonday) > 5 'Saturday or Sunday
InvDate = InvDate - 1
Loop

Access VBA recordCount returning 0 when records exist

I'm attempting to create a function to add n workdays to a date. This obviously needs to take into account weekends and holidays. The weekend part works fine, but I used a table to list all of the holidays and I can't get my query to pull the accurate recordCount. It insists the count is always zero. Here is my code:
Public Function dateAddNoWeekends(dt1 As Date, genDate As Integer)
Dim i As Integer
Dim date1 As Date
Dim date2 As Date
Dim holidays As Boolean
Dim isWeekday As Boolean
Dim dayOfWeek As Integer
Dim numDays As Integer
Dim db As Database
Dim rs As DAO.Recordset
Dim sql As String
Set db = CurrentDb
date1 = dt1
date2 = date1
i = 0
numDays = genDate
sql = "SELECT * FROM Holidays WHERE holidayDate = " & date2
Set rs = db.OpenRecordset(sql)
Debug.Print date2
Do While i < numDays
If Not isWeekday Then date2 = date2 + 1
dayOfWeek = Weekday(date2)
If rs.RecordCount > 0 Then
Debug.Print rs.RecordCount
holidays = True
Else
holidays = False
Debug.Print rs.RecordCount
End If
If dayOfWeek > 1 And dayOfWeek < 7 And holidays = False Then
If i < numDays - 1 Then date2 = (date2 + 1)
isWeekday = True
i = i + 1
Else: isWeekday = False
End If
Loop
rs.Close
Debug.Print date2
To solve this, I declared a variable at the top of the loop (so that it would update correctly each time) and assigned a Dcount function to count from date2 to date2.

Loop in VBA Skipping Values

I have the following code in Access VBA.
Public Sub CalculateVol()
Dim vol As Double
Dim rs As Recordset
Dim rs2 As Recordset
Dim iRow As Long, iField As Long
Dim strSQL As String
Dim CurveID As Long
Dim MarkRunID As Long
Dim MaxOfMarkAsofDate As Date
Dim userdate As String
DoCmd.RunSQL "DELETE * FROM HolderTable"
'Clears out the old array from the holder table.
Dim I As Integer
Dim x As Date
userdate = InputBox("Please Enter the Date (mm/dd/yyyy)")
x = userdate
Dim BucketTermAmt As Long
BucketTermAmt = InputBox("Please Enter the Term Amount")
For I = 0 To 76
MaxOfMarkAsofDate = x - I
strSQL = "SELECT * FROM VolatilityOutput WHERE CurveID=" & Forms!Volatility.cboCurve.Value & " AND MaxOfMarkAsofDate=#" & MaxOfMarkAsofDate & "# ORDER BY MaxOfMarkasOfDate, MaturityDate"
Set rs = CurrentDb.OpenRecordset(strSQL, Type:=dbOpenDynaset, Options:=dbSeeChanges)
Set rs2 = CurrentDb.OpenRecordset("HolderTable")
If rs.RecordCount <> 0 Then
rs.MoveFirst
rs.MoveLast
Dim BucketTermUnit As String
Dim BucketDate As Date
Dim MarkAsOfDate As Date
Dim InterpRate As Double
Dim b As String
b = BucketTermAmt
BucketTermUnit = Forms!Volatility.cboDate.Value
BucketDate = DateAdd(BucketTermUnit, b, MaxOfMarkAsofDate)
InterpRate = CurveInterpolateRecordset(rs, BucketDate)
rs2.AddNew
rs2("BucketDate") = BucketDate
rs2("InterpRate") = InterpRate
rs2.Update
End If
Next I
vol = EWMA(0.94)
Forms!Volatility!txtVol = vol
Debug.Print vol
End Sub
The basic idea is that the user inputs a date for MaxofMarkAsofDate. The code then finds that instance of MarkAsofDate in the table VolatilityOutput, and uses it as a reference point to calculate InterpRate. It stores this number in the HolderTable. Then it loops the same procedure, except using one day previous to the user-inputted MarkAsofDate, and then one day previous to that, and so on for a total of 76 times.
The first part works fine but the loop is giving me trouble. If it doesn't find the user-inputted date in the table, it'll just skip it, but still count it as a loop. So while I want 76 data points, I might only end up with 56, for example, if it skips 20 dates. So I want to either stop it from skipping, or just keep looping until HolderTable has a total of 76 numbers in it. How do I do this?
Sounds like you want a while loop since the for loop as written will always go the same number of times. Looks like you might need a second counter to increment your date.
while count < 76
'get rs here
if rs.RecordCount <> 0 Then
'do everything else
count = count + 1
end if
dateCounter = dateCounter + 1
loop

Get week number from datetimepircker

How to get the number of the week in datetimepicker just like in Mysql just use the function WEEK()
and it will return the week number, simple as that.
example.
datetimepicker1.text = "9/9/2014"
'returns 36
just like in mysql. example.
WEEK(9/9/2014)
`returns 36
You can use Calendar.GetWeekOfYear
Dim dt = datetimepicker1.Value
Dim dateInfo = Globalization.DateTimeFormatInfo.CurrentInfo
Dim c = dateInfo.Calendar
Dim week = c.GetWeekOfYear(dt, dateInfo.CalendarWeekRule, dateInfo.FirstDayOfWeek)

how to convert string to mysql date format using vb.net?

i want to insert a string as date format in mysql table using vb.net.my code is given below.but i always get an error .the string is stored in an array.
mycode
------
Dim sdfsa As String = Convert.ToDateTime(newarray(i + 17)).ToShortDateString().ToString("yyyy-MM-dd")
Dim newdate As Date = DateTime.ParseExact(sdfsa, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)
datarw("date_of_birth") = newdate
Are the Users inputting this information directly?
if they are why don't you input have them input the value in a masked text box?
This Link Could Help
Try this:
my_date =Format(Now,"yyyy-MM-dd")
Example
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim year, month, day As Integer
Dim date_value As Date
Dim date_final_to_mySQL As String
date_value = DateTimePicker1.Value
year = date_value.Year
month = date_value.Month
day = date_value.Day
date_final_to_mySQL = year & "-" & month & "-" & day
MessageBox.Show(date_final_to_mySQL)
End Sub
Only need create a button1 and datetimepicker1
Final Result