Excel :Handling different date format to a standard date format - vb.net

We have a requirement to handle data feeds from different sources( geographies) and load it in standard format.
Difficulty we are facing in handling excel date formats.
We are currently using a add-in to convert the date formats, as below:
Private Function myFormat(ob As Range)
If (IsDate(ob)) Then
' Most times a date is a date but sometimes in excel numbers that are formatted may also return true for IsDate()
' To avoid this case force the conversion to a number and see if it matches. If it does then it's really a number and not a date.
If (Val(ob) = ob) Then
' it's really a number
myFormat = CStr(ob.Value)
Else
myFormat = Format(ob.Value, "YYYY-MM-DD")
End If
Else
If (InStr(ob.Value, ",") > 0) Then ' we must quote any string with a comma
myFormat = Chr(34) & CStr(ob.Value) & Chr(34)
Else
myFormat = CStr(ob.Value)
End If
If (InStr(ob.Value, """") > 0) Then ' we must escape an embedded "'s as well as quote the whole string
myFormat = Chr(34) & Replace(myFormat, Chr(34), Chr(34) & Chr(34)) & Chr(34)
End If
End If
End Function
Issue : It is unable to handle date format as dd.mm.yyyy [ considering it as string ].
I have searched the forum, but could not get a definite solution using vb code.
Quick help will be highly appreciated!
Best Regards,
Swati

Related

Access VBA DCount data type mismatch in criteria expression when data types are obviously the same (both Text)

I'm doing a simple DCount, just looking for how many people have signed up for the same date and time. At the moment I'm just using a single date and a single time to prove the process, then I'll have it loop through all possible dates and times.
Private Sub Get_Singles()
Dim TestDate As String
Dim TestTime As String
AloneCnt = 0
Dinc = 0
Tinc = 0
TestDate = vStartDate
TestTime = "0700"
If (DCount("[ID]", VigilTable, "[fldTime] = " & TestTime) = 1) Then
' "[fldDate] = " & TestDate) & " And
AloneCnt = AloneCnt + 1
End If
End Sub
It works fine for the date (I've moved it to a different line and commented it out so I can focus on the time.).
In the table, fldDate and fldTime are both set for text (shows up as Field Size
= 255 in the properties list) and, as you can see, TestDate and TestTime are both dimmed as String.
And it works if I change the DCount line to:
(DCount("[ID]", VigilTable, "[fldTime] = '0700'")
So where's the error?
Thanks.
The error is, that you must handle date and time as data type Date. So change the data type of the fields to DateTime and:
Dim TestDate As Date
Dim TestTime As Date
Dim AloneCnt As Long
TestDate = vStartDate
TestTime = TimeSerial(7, 0, 0)
If DCount("*", "VigilTable", "[fldTime] = #" & Format(TestTime, "hh\:nn\:ss") & "# And [fldDate] = #" & Format(TestDate, "yyyy\/mm\/dd") & "#") = 1 Then
AloneCnt = AloneCnt + 1
End If
Because you hold all date/time bits as text/string, you should use the text syntax in the criteria as you showed: with quotes (notice the quotes around time string):
(DCount("[ID]", VigilTable, "[fldTime] = '" & TestTime & "'")
You could also keep Date/times in date types like #Gustav's answer.
Update:
You must be doing something wrong; here is my suggested version in VBA window, and it is not Red.
OK, I found the fix after searching several other sites; it was, as I suggested above, a problem of handling variables:
If (DCount("[ID]", VigilTable, "[fldDate]='" & TestDate & "'" & " AND [fldTime] = '" & TestTime & "'") = 1) Then
I always have trouble with those damned single quotes and can never get them in the right place (I'd tried placing single quotes several times but never got them where they are here.); I don't know if the rules are different in different places or I just don't understand the rules or both.
Thanks for all your suggestions; even things that don't work help.

Inputbox is not accepting double number VBA excel

I have a declaration like number= InputBox("Number for:", "Number:"), number is declared as Dim number As Double but when I enter a double number, for example 5.4, into the Inputbox and transmit it into a cell, the cell shows me 54, it deletes the point.
How can I fix this?
THX
If you want to detect which settings your Excel uses for the Decimal seperator, try the code below:
MsgBox "Excel uses " & Chr(34) & Application.DecimalSeparator & Chr(34) & " as a decimal seperator"
if you want to change it to ., then use the line below:
Application.DecimalSeparator = "."
Unfortunately, VBA is horrible at handling differences in decimal seprators. In your case, you should probably use a comma (,), instead of a punctuation/dot (.).
Edit: Using the Application.DecimalSeparator method, it now works regardless of regional settings. Be aware though, it seems to cause some issues if you change the comma separator settings for Excel (it seems that VBA somewhat ignores this setting). If you do not change that however, the example should work in all other cas
Sub GetNumberFromInputBox()
Dim val As String
Dim num As Double
'Get input
val = InputBox("Number for:", "Number:")
Debug.Print Application.DecimalSeparator
If IsNumeric(val) Then
'Try to convert to double as usual
num = CDbl(val)
'If the dot is removed automatically, then
'you will se a difference in the length. In
'those cases, replace the dot with a comma,
'before converting to double
If Len(val) <> Len(num) Then
If Application.DecimalSeparator = "," Then
num = CDbl(Replace(val, ".", ","))
Else
num = CDbl(Replace(val, ",", "."))
End If
End If
'Pring the number
Debug.Print "You selected number: " & num
Else
'If its not a number at all, throw an error
Debug.Print "You typed " & val & ", which is not a number"
End If
End Sub

Using VBA in Access how do I use Dcount to count the number of record that contain this formula - DatePart("y", Now)?

I want to go through the records in a table to see how many already contain the Julian date of today. I want it to be a wildcard search because my project numbers will be in this format "16-2101". The Julian date is in the middle (210).
My code is:
Private Sub AddProjectNum_Click()
TwoDigitYear = Mid$(CStr(DatePart("yyyy", Now)), 3, 2)
dayOfyear = DatePart("y", Now)
CountofProjectsToday = DCount("[ProjectNumber]", "Table1", "[ProjectNumber] Like '*dayOfyear*'")
If CountofProjectsToday = 0 Then
Me.ProjectNum.Value = TwoDigitYear & "-" & dayOfyear & 1
Else
Me.ProjectNum.Value = TwoDigitYear & "-" & dayOfyear & CountofProjectsToday + 1
End If
End Sub
If I were to type the actual Julian date (210) in the place of "dayOfyear" the code works. It doesn't like the reference and I don't know how to get around it.
Try
CountofProjectsToday = DCount("[ProjectNumber]", "Table1", "[ProjectNumber] Like '*" & CStr(dayOfyear) & "*'")
' ^^^^^^^^^^^^^^^^^^^^^^^
That converts your VBA dayOfYear into a string (using CStr), then pastes the resulting string into your query (& ... &).

query in Vba using cdate function with variable

Using the following query in microsoft access sql view works nice and easy with a hard coded date
SELECT Salary.First, Salary.Last,FROM Salary, allowances
WHERE Salary.PayThroughDate = CDate("2014-05-06") AND Salary.SSN = allowances.SSN
but embedding this query in Vba using a variable instead of a hard coded date is another business. It is just not working:
Dim ddate As Variant
Dim getDay As Integer
Dim getMonth As Integer
Dim getYear As Integer
getDay = Day(Me.DTPicker2.Value)
getMonth = Month(Me.DTPicker2.Value)
getYear = Year(Me.DTPicker2.Value)
ddate = getDay & "/" & getMonth & "/" & getYear
ddate = Format(ddate, "dd/mm/yyyy")
query1 = "SELECT Salary.First, Salary.Last FROM Salary, allowances WHERE Salary.PayThroughDate = " & CDate(ddate) & " AND Salary.SSN =
allowances.SSN
Any ideas in this Vba Sql mix? Am I missing single or double quotes?
When you send the SQL directly to JET and not running it from the normal user-interface of MS Access, you will need to make the date-format in the American format. I have had this problem a lot long time ago, but solved it by using this function to format my date in to text, the way that JET expects it to be:
Function SQLDate(varDate As Date) As String
'---------------------------------------------------------------------------------------
'Purpose Formats a date in the american way so that it can be used in
' SQL (by the JET-engine)
'Accepts varDate - the date that should be converted to text
'Returns The date converted to text, in american format with both the day and
' the month using 2 characters. (01 is january)
'---------------------------------------------------------------------------------------
'Changelog
'HANY 20100407: Stopped using the FORMAT-function for the date-value, as I found some
' cases where the format did not return the date as specified.
'---------------------------------------------------------------------------------------
'SQLDate = "#" & Format$(varDate, "mm\/dd\/yyyy") & "#"
SQLDate = "#" & Format(Month(varDate), "00") & "/" & Format(Day(varDate), "00") & "/" & Format(Year(varDate), "0000") & "#"
End Function
In your example above, you now add the date like this & SQLDate(CDate(ddate)) & instead of & CDate(ddate) &
The equivilant query is:
query1 = "SELECT Salary.First, Salary.Last
FROM Salary, allowances
WHERE (Salary.PayThroughDate =Cdate(" & ddate & ") )
AND (Salary.SSN = allowances.SSN)"
The problem you had is that when you constructed the sql string, the cdate value was converted into a string value first. It was just the same as
WHERE (Salary.PayThroughDate =" & format(ddate,"general date") & ")
If you do the cdate conversion later it works. Another almost equivilant query is
...
WHERE Salary.PayThroughDate =#" & ddate & "#
...
Both Cdate and the # tags take a string date, and convert to a date type (using slightly different rules). Since a date type is actually stored as the integer part of a double, you can also do:
WHERE Salary.PayThroughDate =cdbl(#" & ddate & "#)
or
WHERE Salary.PayThroughDate =clng(#" & ddate & "#)
or
WHERE Salary.PayThroughDate =" & clng(cdate(ddate) & "
--the number will be converted into a string to be part of the sql string, but numbers work correctly in SQL: they don't need to have # tags or conversion functions to make them work.
but you started out with a date value:
v =Me.DTPicker2.Value
so you can convert that directly to a number without converting it to a string first:
d = clng(Me.DTPicker2.Value)
...
WHERE (Salary.PayThroughDate = " & d & ")
which is the fastest and least error prone of handling dates in Access/VBA, but comes undone when you start working with SQL server, which stores dates differently.
Also, I see that you correctly used ISO date format when constructing your test example, but switched to American date format when constructing a string from the date value. That was a mistake. To avoid errors and 'evil date guessing' in SQL, you should stick to the ISO format:
v =Me.DTPicker2.Value
sdate = format(v,"yyyy-mm-dd")

How to run query, automate using VBA Macro and Excel, make "loading" feature while reconciling?

I am building a reconciliation tool via VBA that automates queries from my oracle database and a worksheet. When I run the query I want the user to input what ITEM (in this case pipeline) to query (the worksheet has many items) and the end/start dates. I am having trouble figuring out the following:
1) It is querying - if the value is NULL, how may I tell it to print out "DATA NOT AVAILABLE"
2) How can I clear up the old output from pipeline A, when I am querying pipeline B?
3) My dates are saved as strings in Oracle - how can I convert that to date?
Thank you!
Here is what I have so far:
Option Explicit
Option Base 1
Dim cnnObject As ADODB.Connection
Dim rsObject As ADODB.Recordset
Dim strGPOTSConnectionString As String
Dim startDate As Date
Dim endDate As Date
Dim strPipelineName As String
Dim strQuery As String
Sub ClickButton2()
Debug.Print ("Button has been clicked")
Dim Pipeline As String
Dim DateStart As Date
Dim DateEnd As Date
Pipeline = InputBox("Enter PipeLine", "My Application", "Default Value")
DateStart = InputBox("Enter Start Date", "My Application", DateTime.Date)
DateEnd = InputBox("Enter End Date", "My Application", DateTime.Date + 1)
Pipeline = Range("B1").Value
DateStart = Range("B2").Value
DateEnd = Range("B3").Value
strQuery = "select pipelineflow.lciid lciid, ldate, volume, capacity, status, " & _
"pipeline, station, stationname, drn, state, county, owneroperator, companycode, " & _
"pointcode, pottypeind, flowdirection, pointname, facilitytype, pointlocator, " & _
"pidgridcode from pipelineflow, pipelineproperties " & _
"where pipelineflow.lciid = piplineproperties.lciid " & _
"and pipelineflow.audit_active = 1 " & _
"and pipelineproperties.audit_active =1 " & _
"and pipelineflow.ldate >= '" & Format(DateStart, "dd-MMM-yyyy") & "' and pipelineflow.ldate < '" & Format(DateEnd, "dd-MMM-yyyy") & "' " & _
"and pipelineflow.ldate >= '" & DateStart & "' and pipelineflow.ldate < '" & DateEnd & "' " & _
"and pipelineproperties.pipeline = '" & Pipeline & "' "
Call PullZaiNetData(strQuery)
Call TieOut
End Sub
Sub PullZaiNetData2(ByVal strQry As String)
Set cnnObject = New ADODB.Connection
Set rsObject = New ADODB.Recordset
strGPOTSConnectionString = "DRIVER={Microsoft ODBC for Oracle}; SERVER=hhh; PWD=hhhh; UID=hhh"
cnnObject.Open strGPOTSConnectionString
rsObject.Open strQry, cnnObject, adOpenStatic
Worksheets("ZaiNet Data").Cells(1, 1).CopyFromRecordset rsObject
rsObject.Close
cnnObject.Close
Set rsObject = Nothing
Set cnnObject = Nothing
End Sub
Sub TieOut()
End Sub
Since you changed your questions, I'll add another answer.
1) It is querying - if the value is NULL, how may I tell it to print out "DATA NOT AVAILABLE"
Which value? I suspect that you mean when the query returns no records. To check this, test for rsObject.RecordCount = 0:
Dim ws As Worksheet
Set ws = Worksheets("ZaiNet Data")
ws.UsedRange.Clear '' remove results of previous query if any
If rsObject.RecordCount = 0 Then
ws.Cells(1, 1) = "DATA NOT AVAILABLE"
Else
ws.Cells(1, 1).CopyFromRecordset rsObject
End If
You can also test for one or both of rsObject.BOF or rsObject.EOF being true ("Beginning Of File" or "End Of File" respectively).
When developing things in VBA, especially when using new features that I'm unfamiliar with, I do lots of tests that output things to the Immediate Window. To help with this, I use the following little routine:
Sub Say(s as String)
Debug.Print s
End Sub
It makes it a little easier to generate testing output that typing "Debug.Print" all the time (even slightly easier than typing "Debug.P" + Enter using Intellisense).
So when you open your recordset, show the record count after it:
rsObject.Open strQry, cnnObject, adOpenStatic
Say rsObject.RecordCount & " records"
Do something like this any time you want to verify a value.
Later on, if you want to capture your debugging statements in a text file, you just need to change the operation of the Say() routine.
2) How can I clear up the old output from pipeline A, when I am querying pipeline B?
As shown in context above:
ws.UsedRange.Clear '' remove results of previous query if any
3) My dates are saved as strings in Oracle - how can I convert that to date?
You don't technically need to convert the resulting date strings to date values, you may find that just by putting them in a cell, Excel will treat them as date values.
You just need to make sure that the user's dates get converted to the format that the database is expecting.
Your query string as it stands above still shows two lines incorporating the user's dates. The one that uses Format() to convert them to "dd-MMM-yyyy" format is the one you want to keep. Delete the other line, making sure your string concatenating syntax is still correct.
To actually convert the date string to a date value though, you would use the CDate() function:
Sub DateTest()
Dim sDate As String
Dim dDate As Date
sDate = "09-Jul-2009"
dDate = CDate(sDate)
Say "sDate = " & sDate
Say "dDate = " & dDate
dDate = dDate + 1
Say "dDate = " & dDate
End Sub
Immediate Window output:
sDate = 09-Jul-2009
dDate = 7/9/2009
dDate = 7/10/2009
We can verify that it converted the string to a date value because it shows up in the default date format for my machine, and responds to date math (adding 1 day).
Answers to previous questions (paraphrased):
1) "how to make sure end date is after start date":
Valid date values are floating point numbers, so DateEnd should be >= DateStart. The whole number part is the number of days since 1900-01-01. The fractional part is the current time of day (eg 12 noon = 0.5).
2) "use fancy calendar entry controls for dates"
Look at the controls available under the Insert> Object menu (in Excel 2003 and earlier - it's in 2007 too, but in a different place). One of them is a Calendar control. Double-clicking it in the Objects list will insert it into the current cell and put the sheet into Design Mode. Right click the control and choose Properties. Type a cell address into the LinkedCell field. Then click the "Exit Design Mode" button from the little toolbar that should have popped up. Now when you select a date on the control, it will show the value in the cell you linked it to.
Similarly there is a drop down list control that you can use to select your pipeline types.
3) "why am I getting an error on DateEnd = Range("B3").Value?"
The DateEnd error is probably due to a missing or invalid value in the cell you specified, as I asked in my comment.
What version of Excel are you doing this in? Excel 2003