473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Can a String be converted into a Date field in VBA?

133 100+
I need to calculate a value for a date field. However, the month is evaluated to determine the start and end date. Therefore, the fields are declared as strings. The values are moved in for the month, day and year. Thereafter, the string needs to be converted into a date field to query on the date.

Any assistance, would be greatly appreciated.

The following is my current code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub txt_MM_cmb_AfterUpdate()
  2.  
  3. On Error GoTo Err_Hndlr
  4.  
  5.     Dim txt_MM As String
  6.     Dim txt_dd_start As String
  7.     Dim txt_dd_end As String
  8.     Dim txt_yy As String
  9.     Dim txt_start As String
  10.     Dim txt_end As String
  11.  
  12.  
  13.     txt_MM = " "
  14.     txt_dd_start = " "
  15.     txt_dd_end = " "
  16.     txt_yy = "20" & [Forms]![F_Waiver_Yr]![Txt_Waiver_Yr]
  17.     txt_start = " "
  18.     txt_end = " "
  19.  
  20.  
  21.  
  22.     txt_dd_start = "01"
  23.  
  24.     Select Case Forms!F_Waiver_Yr!txt_MM_cmb
  25.         Case Is = "January"
  26.             txt_MM = "01"
  27.             txt_dd_end = "31"
  28.         Case Is = "February"
  29.             txt_MM = "02"
  30.             Dim Date1 As Date, Date2 As Date, IsLeapYear As Boolean
  31.             Date1 = "January 1," & Year(Now()): Date2 = "December 31," & Year(Now())
  32.             If DateDiff("d", Date1, Date2) = 365 Then
  33.                 IsLeapYear = True
  34.                 txt_dd_end = "29"
  35.                 MsgBox " This is a Leap Year"
  36.             Else
  37.                 txt_dd_end = "28"
  38.             End If
  39.         Case Is = "March"
  40.             txt_MM = "03"
  41.             txt_dd_end = "31"
  42.         Case Is = "April"
  43.             txt_MM = "04"
  44.             txt_dd_end = "30"
  45.         Case Is = "May"
  46.             txt_MM = "05"
  47.             txt_dd_end = "31"
  48.         Case Is = "June"
  49.             txt_MM = "06"
  50.             txt_dd_end = "30"
  51.         Case Is = "July"
  52.             txt_MM = "07"
  53.             txt_dd_end = "31"
  54.         Case Is = "August"
  55.             txt_MM = "08"
  56.             txt_dd_end = "31"
  57.         Case Is = "September"
  58.             txt_MM = "09"
  59.             txt_dd_end = "30"
  60.         Case Is = "October"
  61.             txt_MM = "10"
  62.             txt_dd_end = "31"
  63.         Case Is = "November"
  64.             txt_MM = "11"
  65.           txt_dd_end = "30"
  66.         Case Else
  67.             txt_MM = "12"
  68.             txt_dd_end = "31"
  69.     End Select
  70.  
  71.     txt_start = txt_MM & txt_dd_start & txt_yy
  72.     txt_end = txt_MM & txt_dd_end & txt_yy
  73.  
  74.     txt_start = Format(CDate([txt_start]), "mm/dd/yyyy")
  75.     txt_end = Format(CDate([txt_start]), "mm/dd/yyyy")
  76.  
  77.  
  78. txt_MM_cmb_AfterUpdate_Exit:
  79.  
  80.   Exit Sub
  81.  
  82.  
  83. Err_Hndlr:
  84.     MsgBox "[" & Err.Number & "]:  " & Err.Description, vbInformation, "txt_MM_cmb_AfterUpdate()"
  85. End Sub
  86.  
Sep 13 '10 #1
5 1524
Oralloy
988 Expert 512MB
A quick example of using CDate().

Expand|Select|Wrap|Line Numbers
  1. Private Sub TEST_CDate()
  2.   Dim myString As String
  3.   Dim myDate As Date
  4.   myString = "February 12, 1969  11:59:59 PM"
  5.   myDate = CDate(myString)
  6. End Sub
Sep 13 '10 #2
dowlingm815
133 100+
Oralloy,

The string data would need to be converted from the form of "02/28/2010" to a date field "02/28/2010." from what i understand of CDate it converts the spelled out date, therefore, my date would be 02/12/1969. i looked into this hopefully and was disappointed since it doesn't actually convert a numeric string.

Thanks anyway.

mary
Sep 13 '10 #3
Oralloy
988 Expert 512MB
Mary,

I just tried it with your date in Access 2007, and it looks like the date converted correctly in the locals window.

Give it a try and see?

Luck!
Sep 13 '10 #4
dowlingm815
133 100+
Oralloy,

My apologies for the delayed response. However, the simplest approach was to move the string value into the date field.

Thank you for your help.

Mary
Sep 20 '10 #5
Oralloy
988 Expert 512MB
Mary,

No problems. Hopefully it helped a little.

I often prefer explicit conversions to implicit conversions, as it lets me know what's going on in the code when I have to revisit it later. I'm a little gun-shy about conversions because I've had to debug and clean up lots of code where implicit conversions worked in the "success" cases, and caused terrible things to happen when bogus values are entered by the user.

Anyway - I sure hope your project is working out well.

Cheers!
Sep 20 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Noyb | last post by:
How do I convert a string into a value I can upload to mysql date field. Specifically, I have a user select a month, day and year from drop-downs, then I want them submitted to one date field....
1
by: Droope | last post by:
I have a date field in a SQL table that allows null. If certain conditions are met in the user interface I need to set the field to nulls. using a typed dataset I do the following Dim...
4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
3
by: usenet | last post by:
I have inherited a table where date information was saved from PHP as a VARCHAR. (Sigh.) This means that the so-called date fields look like this : August 1, 2005, 9:09 am EDT October 13, 2004,...
3
by: Fran Zablocki | last post by:
I have a process that exports an Access table to a comma-delimited text file. One of the fields that is exported shows the date it was exported, using the Date() function. In the Access table, the...
3
by: Kevin Hodgson | last post by:
I'm trying to import from a text file into a Sql DB, but I'm getting hung up on a date field. My text file has the date in the dd/mm/yyyy format. I Cast that field to CDate in VB.NET, which sets...
1
by: itfgnanvigneshwari | last post by:
hai, as i am new to php field i had a problem in date field. i have table named student with fields fieldname type name varchar...
10
by: satishrajana | last post by:
Hi, My SQL returns a NULL in a datefield if there is no date in that field. If there is a NULL in this column, I want to replace it with spaces in my SELECT statement when I am selecting these...
1
by: Thorben Grosser | last post by:
Hey again newsgroup, I am still working on an archive database and got another problem: I have to classify the folders by a date or a date range which has the format mm/yyyy I got the feeling,...
10
by: veer | last post by:
Hi I have a probleum in vconverting a string value into date in vb.net e.g str="31.12.2005" i want to convert it into date format like "mm/dd/yyyy" i use CDATE function for it but every time...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.