I need to convert the date from mm/dd/yyyy to dd/mm/yyyy in vbscript - date

So far I have this:
FormatDateTime(negativeItemsRS("ItemDate"), 0)
Its displaying the date in the format of mm/dd/yyyy. I want to convert that to a dd/mm/yyyy
Please help not sure how to do this.
Thanks,

You have to set the locale id to one that uses the date format that you want. I don't remember which format used where, but either UK (2057) or US (1033) should work.
You haven't specified your environment. In ASP you could use the LCID property in the Language directive or in the Session or Response classes, depending on what scope you want for the setting:
<%#Language="VBScript" LCID="1033"%>
or
Session.LCID = 1033
or
Response.LCID = 1033

To change the date from MM/DD/YYY to DD/MM/YYYY in VB Script very simple steps can be used as given below
Let say "date1" (MM/DD/YY) = 03/06/2014
I want "date2" to be in DD/MM/YY as 06/03/2014
d = Day(date1)
m = Month(date1)
y = Year(date1)
date2 = d & "/" & m & "/" & y
Will give the required result.

What you need is a FormatDate function. I used to do this the hard way with manual concatentation, but I discovered that there are a few .NET libraries that are accessible from COM and thus from ASP Classic. My version leverages the fact that I have a StringBuilder class which is a wrapper around the StringBuilder class in .NET.
'******************************************************************************
Public Function FormatDate( sFormat, dDateValue )
'PURPOSE: To format a date with any arbitrary format
'ARGS:
' sFormat is the defined formats as used by the .NET Framework's System.DateTimeFormatInfo.
' Note that this format is case-sensitive.
'CALLS:
' 1. System.Text.StringBuilder class in the .NET Framework.
'EXAMPLE CALL:
' Dim sFormatedDate
' sFormatedDate = FormatDate( "MM/dd/yy", "1/1/1900 12:00 AM" )
' Or
' sFormatedDate = FormatDate( "MM/dd/yyyy", "1/1/1900 12:00 AM" )
'DESIGN NOTE:
' This function leverages the fact that System.Text.StringBuilder is COMVisible.
' Thus, we can call its AppendFormat function from here.
' You can find more information about the format string parameters allowed at
' http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
Dim oStringBuilder
Dim sSbFormatString
Dim dDate
If Not IsDate( dDateValue ) Then
FormatDate = vbNullString
Exit Function
End If
On Error Resume Next
dDate = CDate(dDateValue)
If Err.number <> 0 Then
FormatDate = vbNullString
Exit Function
End If
'if an empty format string is passed, then simply return
'the value using the default shortdate format.
If Len(sFormat & vbNullString) = 0 Then
sSbFormatString = "{0:d}"
Else
sSbFormatString = "{0:" & sFormat & "}"
End If
Set oStringBuilder = CreateObject("System.Text.StringBuilder")
Call oStringBuilder.AppendFormat(sSbFormatString, dDate)
FormatDate = oStringBuilder.ToString()
Set oStringBuilder = Nothing
End Function
'**************************************************************************
' Use this class to concatenate strings in a much more
' efficient manner than simply concatenating a string
' (strVariable = strVariable & "your new string")
Class StringBuilder
'PURPOSE: this class is designed to allow for more efficient string
' concatenation.
'DESIGN NOTES:
' Originally, this class built an array and used Join in the ToString
' method. However, I later discovered that the System.Text.StringBuilder
' class in the .NET Framework is COMVisible. That means we can simply use
' it and all of its efficiencies rather than having to deal with
' VBScript and its limitations.
Private oStringBuilder
Private Sub Class_Initialize()
Set oStringBuilder = CreateObject("System.Text.StringBuilder")
End Sub
Private Sub Class_Terminate( )
Set oStringBuilder = Nothing
End Sub
Public Sub InitializeCapacity(ByVal capacity)
On Error Resume Next
Dim iCapacity
iCapacity = CInt(capacity)
If Err.number <> 0 Then Exit Sub
oStringBuilder.Capacity = iCapacity
End Sub
Public Sub Clear()
Call Class_Initialize()
End Sub
Public Sub Append(ByVal strValue)
Call AppendFormat("{0}", strValue)
End Sub
Public Sub AppendFormat(ByVal strFormatString, ByVal strValue)
Call oStringBuilder.AppendFormat(strFormatString, (strValue & vbNullString))
End Sub
'Appends the string with a trailing CrLf
Public Sub AppendLine(ByVal strValue)
Call Append(strValue)
Call Append(vbCrLf)
End Sub
Public Property Get Length()
Length = oStringBuilder.Length
End Property
Public Property Let Length( iLength )
On Error Resume Next
oStringBuilder.Length = CInt(iLength)
End Property
'Concatenate the strings by simply joining your array
'of strings and adding no separator between elements.
Public Function ToString()
ToString = oStringBuilder.ToString()
End Function
End Class
So, with this class you could do something like:
FormatDate("dd/MM/yyyy", RS("DateField"))
Note that the string passed in is case-sensitive.
EDIT I see that at some point I amended my FormatDate function to eliminate the use of my VBScript StringBuilder class and instead just use the .NET class directly. I'll leave the VBScript StringBuilder class in there for reference in case anyone is interested. (I did swap the order of the two however to make the code that appears at the top more applicable to the problem).

Related

Libreoffice calc macro to get current date + 14 days into a cell

I need to get todays date + 14 days string formatted in standard dd.mm.yyyy format into Libre Office calc cell.
I already tried the code below, but I lack the knowledge to cope with "Object variable not set" error.
REM ***** BASIC *****
sub Datumplus14
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
Dim cell as object
dim term as date
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
term = today()
cell.String = DateAdd("d", 14, datum)
dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, cell)
end sub
Different ideas on how to get this done instead of macro is welcome as well.
Hmm, not sure if this will answer the specifics of what you're actually trying to do, but you can easily get the current date and just +14 to it directly in a cell formula.
Like so:
=NOW()+14
The rest is just applying the required date format to that cell. You can also grab the date from another cell too.
Function myFunction() As String
myFunction = Format(Now()+14, "dd.mm.yyyy")
End Function

DoCmd.BrowseTo acBrowseToForm MULTIPLE WHERE conditions

I am new to Access 2010 VBA but have solid SQL background. I am trying to open/browse a form from a toogle button based on complex filter.
The form is called: FormSuivi
In SQL, the filter would be like this:
WHERE Randomise = 'Y' AND ActualSxDate is not null
AND datediff('d', Date(),ActualSxDate) > 140 AND DCD = 0;
In this Accessdatabase, the following field's types are:
Randomise: text
ActualSxDate: Date
DCD: Yes/no -> integer (-1/0)
For now, all I managed to do is to implement one condition at a time:
Private Sub Toggle25_Click()
DoCmd.BrowseTo acBrowseToForm, "FormSuivi", , "Randomise = """ & "Y" & """"
End Sub
How can all the conditions listed in SQL be squeezed into a VBA command line?
The parameter WhereCondition can be a full WHERE string, without the WHERE keyword. Including ANDs, parentheses, etc.
Single quotes ' help to keep the string readable (as opposed to """ constructs).
Variables need to be concatenated, e.g.
Dim S As String
S = "Randomise = '" & strRandomise & "' AND ActualSxDate is not null " & _
"AND datediff('d', Date(),ActualSxDate) > 140 AND DCD = " & bDCD
DoCmd.BrowseTo acBrowseToForm, "FormSuivi", , S

Runtime error '91' on visual basic for executing matlab .m file

I want to call matlab & at the same time run .m file from visual basic 6. but I'm getting this run-time error '91', variable not set bla bla. I've searched the internet to find any solution but I couldn't. There is something wrong with my code, I don't know what it is. can anyone please check & see what's wrong?
Private Sub Form_Load()
Dim MatLab As Object
Dim Result As String
Dim MReal(1, 3) As Double
Dim MImag(1, 3) As Double
Dim mat_exe As String
Dim mat_io_folder As String
Dim mat_m As String
mat_exe = "G:\matlab\bin\matlab.exe"
mat_io_folder = "G:\Farin\New folder"
mat_m = "Untitled.m"
FileName = mat_exe & " " & "addpath('mat_io_folder') & mat_m" & " -s1"
runmatlab = Shell(FileName, 1)
Result = MatLab.Execute("cd G:\Farin\New folder")
Result = MatLab.Execute("Untitled")
'Calling m-file from VB
'Assuming solve_bvp exists at specified location
'Result = MatLab.Execute("cd G:\Farin\New folder\Untitled")
End Sub
Error 91 in VB6 means object variable not set, which, at a guess, would be the statement
result = MatLab.Execute("...")
Matlab is declared as an object but it has not been assigned a value. List of VB6 runtime errors can be found in https://msdn.microsoft.com/en-us/library/aa264975(v=VS.60).aspx
Another problem is the Filename assignment. It should read
FileName = mat_exe & " " & "addpath('" & mat_io_folder & "') " & mat_m & " -s1"
Might be an idea to MsgBox Filename before running the shell command.

Data validator VBA excel- compare a value within a string in one cell to a value in another

Have not been able to find anything that fits my needs.
I have two columns of values (L and U). Column L contains a file names that includes a date in MM-DD-YYYY format (example yadayadayada thru (03-15-2015).pdf) column U contains a date. What I need to do is have a macro compare the date within the file name to the date in the column U. Other dates may appear within the text in column L but the date I need to compare against is always after "thru" and in parentheses followed by the file extension. If they do not match, I need the value in column U highlighted and replaced with the text "FAIL". I'm going to continue searching but any help is greatly appreciated. Thanks!
Does it have to be VBA? This can be accomplished with Conditional Formatting.
Apply this conditional format formula to column U:
=AND(U1<>"",L1<>"",U1<>--TRIM(MID(SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(L1," ",REPT(" ",255)),255)),")",REPT(" ",255)),2,255)))
And set the number format to Custom "FAIL" with yellow (or the highlight color of your choice) fill.
EDIT
If it has to be VBA, then this should work for you:
Sub tgr()
Const HeaderRow As Long = 1 'Change to your actual header row
Dim ws As Worksheet
Dim rngFail As Range
Dim rngFiles As Range
Dim FileCell As Range
Dim dDate As Double
Set ws = ActiveWorkbook.ActiveSheet
Set rngFiles = ws.Range("L" & HeaderRow + 1, ws.Cells(Rows.Count, "L").End(xlUp))
If rngFiles.Row < HeaderRow + 1 Then Exit Sub 'No data
For Each FileCell In rngFiles.Cells
If Len(Trim(FileCell.Text)) > 0 Then
dDate = 0
On Error Resume Next
dDate = CDbl(CDate(Trim(Mid(Replace(Trim(Right(Replace(FileCell.Text, " ", String(255, " ")), 255)), ")", String(255, " ")), 2, 255))))
On Error GoTo 0
If dDate <> ws.Cells(FileCell.Row, "U").Value2 Then
Select Case (rngFail Is Nothing)
Case True: Set rngFail = ws.Cells(FileCell.Row, "U")
Case Else: Set rngFail = Union(rngFail, ws.Cells(FileCell.Row, "U"))
End Select
End If
End If
Next FileCell
If Not rngFail Is Nothing Then
rngFail.Value = "FAIL"
rngFail.Interior.ColorIndex = 6
End If
End Sub

Displaying a recordset on a form in Access 2010 using VBA

I'm developing a data retrieval application in Access 2010 in which the user chooses which table, columns, and rows to view by selecting listbox entries. The VBA code generates a SQL statement from these choices and then creates an ADBDB.Recordset object from this.
How can I display the recordset records in Access? None of the grid controls work in Access 2010 and the subform just isn't designed for this purpose. Can someone recommend another strategy?
You could save the SELECT statement as a named query, then open the query as a datasheet. It's not really a form, but somewhat form-like.
Call DatasheetFromSql(strSql)
Public Sub DatasheetFromSql(ByVal pSql As String)
Const cstrQuery As String = "qryDiscardMe"
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strMsg As String
On Error GoTo ErrorHandler
Set db = CurrentDb
db.QueryDefs.Delete cstrQuery
Set qdf = db.CreateQueryDef(cstrQuery, pSql)
DoCmd.OpenQuery cstrQuery, , acReadOnly
ExitHere:
On Error GoTo 0
Set qdf = Nothing
Set db = Nothing
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 3265 ' Item not found in this collection. '
Resume Next
Case Else
strMsg = "Error " & Err.Number & " (" & Err.description _
& ") in procedure DatasheetFromSql"
MsgBox strMsg
GoTo ExitHere
End Select
End Sub
I opened the query read-only. If you want to allow users to edit the data returned by their custom queries, I would not recommend this approach. Instead I would invest the effort in the approach HK1 offered because it can support better control of the user data changes.
With the query opened as a datasheet, you can use Screen.ActiveDatasheet to inspect its properties. At least some of the methods are also available to you. For example you could resize/reposition the datasheet like this:
Screen.ActiveDatasheet.Move Left:=0, Top:=0, Width:=(4 * 1440), Height:=(3 * 1440)
The units are twips (1440 twips / inch), so that would make the width 4 in., height 3 in., and move it to the upper left corner of the Access window.
Here's what I think you'll have to do to get this kind of functionality.
You'll first need to create enough of the correct controls on a form to handle every possible scenario. You'll then need to set the form to be a datasheet form so that it appears as a grid.
Now set the controlsource on your controls to correspond with one of the fields in the recordset. On every control that is not in use needs to have it's ColumnHidden property set to true. You'll also have to change the caption of the associated label to show the appropriate column name for each control that will be visible.
Now, bind that form to your ADO recordset object.
Me.Recordset = rst
'or
Me.Subform1.Form.Recordset = rst
Is this a perfect solution? Most certainly not. Access doesn't have anything that compares to the DataGridView in .Net or even to the Grid controls that are used in VB6. In my opinion, you're really pushing the limits of Access to try to get this sort of functionality. It's like swimming upstream. You'll find that everything you do is going to be fairly difficult and some things just aren't going to be possible.
for an ADP Project where you can't have local MS Access query definitions, you can create a datasheet form with multiple textboxs named txt1, txt2,.... txt30 and labels name lbl1 ... lb30 and this code will set the form.recordsource and set the textbox.controlsource and the label.caption to the appropriate fields from an ADO recordset object. This form will allow you to view your ADO Recordset similar to the Docmd.OpenQuery method.
You must pass the ADO recordset's SQL statement to the form using the OpenArgs property of the form. the code below shows the VBA code to call\open the form (which shows the ADO Recordset like a query) and pass your sql string. The vba code on the form's load event will set all the control properties, resize the columns that have data and hide the columns that do not have a corresponding field from the ado recordset:
'stevekirchner 09/29/2012 Replace Access parameterized query with SQL Server in-line function
'DoCmd.OpenQuery "qry_SearchMaster_CaseTitles", , acReadOnly
strsql = "Select * from dbo.UDF__qry_SearchMaster_CaseTitles ('%" & Me.tbxSearchTerm.Value & "%') "
Call Display_ADO_Recordset_from_Datasheet_Form(strsql, "frm_Display_ADO_Recordset_Result1")
'create a non-form module and put the code for the sub Display_ADO_Recordset_from_Datasheet_Form
'and function fIsLoaded in it (this will allow you make several forms to view ADO recordset and
'call the code from one place\module):
Sub Display_ADO_Recordset_from_Datasheet_Form(sSQL As String, sFormName As String)
On Error GoTo Error_Handler
If fIsLoaded(sFormName) Then
DoCmd.Close acForm, sFormName
End If
DoCmd.OpenForm sFormName, acFormDS, , , acFormReadOnly, , OpenArgs:=sSQL
Exit_Sub:
Exit Sub
Error_Handler:
MsgBox Err.Description & " Error No: " & CStr(Err.Number)
Resume Exit_Sub
End Sub
Function fIsLoaded(ByVal strFormname As String) As Boolean
On Error GoTo Error_Handler
'Returns False if form is not open or True if Open
If SysCmd(acSysCmdGetObjectState, acForm, strFormname) <> 0 Then
If Forms(strFormname).CurrentView <> 0 Then
fIsLoaded = True
End If
End If
Exit_Function:
Exit Function
Error_Handler:
MsgBox Err.Description & " Error No: " & CStr(Err.Number)
fIsLoaded = False
Resume Exit_Function
End Function
'Create a datasheet view form (named frm_Display_ADO_Recordset_Result1) with 30 textboxes and 30
'30 labels named txt1 - txt30 and lbl1 - lbl30 and put this code in the form's module:
Option Compare Database
Private Sub Form_Load()
On Error GoTo Error_Handler
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim rsClone As ADODB.Recordset
Dim strsql As String
Set conn = CurrentProject.Connection
Set rs = New ADODB.Recordset
strsql = Me.OpenArgs
rs.Open strsql, conn, adOpenStatic, adLockOptimistic
Set rsClone = rs.Clone
Call Update_Form_Controls("your text goes here", strsql, rsClone)
Exit_Sub:
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
Exit Sub
Error_Handler:
MsgBox Err.Description & "; Error Number : " & Err.Number, vbOKOnly
Resume Exit_Sub
End Sub
Sub Update_Form_Controls(Header_Label As String, SQL As String, CloneRS As Recordset)
Dim rsCount As Integer
Dim i As Integer
On Error GoTo Error_Handler
Me.Form.Caption = Replace(SQL, "Select * From ", "Display: ")
rsCount = CloneRS.RecordCount
If rsCount <= 0 Then
MsgBox "The Query did not return any data to view", vbOKOnly
DoCmd.Close
Else
Me.Form.SetFocus
Me.RecordSource = SQL
i = 1
Do Until i = 31
Me("lbl" & i).Caption = ""
Me("txt" & i).ControlSource = ""
Me("txt" & i).ColumnHidden = True
i = i + 1
Loop
i = 1
With CloneRS
For Each Field In .Fields
On Error Resume Next
Me("lbl" & i).Caption = .Fields(i - 1).Name
Me("txt" & i).ControlSource = .Fields(i - 1).Name
Me("lbl" & i).Visible = True
Me("txt" & i).ColumnHidden = False
Me("txt" & i).SizeToFit
i = i + 1
'Debug.Print Field.Name
On Error GoTo 0
Next Field
End With
End If
Exit_Sub:
Me.Requery
Exit Sub
Error_Handler:
MsgBox Err.Description & "; Error Number : " & Err.Number, vbOKOnly
Resume Exit_Sub
End Sub
I don't use ADO but simply VBA, in which case above approach is wrong: one doesn't need any means for displaying an existing recordset in a form, but should on the contrary define an adequate recordset within the form !
Instead of creating MyDataBase.OpenRecordset ("SELECT … [SQL query] ;"), just set the form's RecordSource to the very same query definition:
Forms![MyDisplayForm].RecordSource = "SELECT … [SQL query] ;"
Forms![MyDisplayForm].Requery
For me this works perfectly (Windows 7 pro ×64 / MS Office pro ×64)