vb.netdatems-accesstype-conversion

String to date error (Conversion from string to type date is not valid)


I want to crate date as MS Access like date (example -> #mm/dd/yyy#) input date can be string or date so I have created object variable to hold value of date. Then I convert it to msaccess date. but it gives error. See the picture attached.

Before posting this question I had searched a lot, but I don't understand the solutions I found. A major solution is ParseExact("Date String", "Format", System. IFormatProvider). But in my editor Intellisense does not recognize culturevalue. It tells me culturevalue is not defined.

Then I tried Date.tryparse(). It worked, but if I have to use this, then what is use of CDate conversion function?

Edit 1 : Date.tryparse() does not work it returns false

in vb6 "#" & Month(dateIn) & "/" & Day(dateIn) & "/" & Year(dateIn) & "#" works perfectly


Solution

  • It seems that you ask for a string expression for a VBA date value.

    That can be done like this:

    Dim DateIn As DateTime
    
    DateIn = DateTime.Today
    
    Dim Rval As String
    
    Rval = DateIn.ToString("'#'MM'/'dd'/'yyyy'#'")
    
    Console.WriteLine(Rval)
    
    ' Output:
    ' #07/10/2022#