I'm trying to get the current month as a short string in a Visual Basic Script:
Dim month
Dim mth
month = Now.Month ' This doesn't work.
month = Now().Month ' Tried this too.
month = Month(Now) ' Also tried this.
mth = MonthName(month, True) ' (e.g. "Apr" or "Mar")
However, I keep getting runtime errors:
Microsoft VBScript runtime error: Object required: 'Now'
Microsoft VBScript runtime error: Object required: 'Now()'
Microsoft VBScript runtime error: Type mismatch 'Month'
I'm able to use Now fine as a string:
CStr(Now)
Or as a plain value
Dim val
val = Now
How can I use Now as an Object for accessing its member functions? Perhaps I am confusing Visual Basic functionality with VB Script?
VBScript Dates are not objects - use the Month(SomeDate) function to get the month number of SomeDate:
>> WScript.Echo Month(Now)
>>
4
To get the month's name (abreviated or full), use:
>> WScript.Echo MonthName(Month(Now()),False)
>> WScript.Echo MonthName(Month(Now()),True)
>>
April
Apr
(stolen from #collapsar, but using the correct (boolean) type of the second parameter).
Official Docs for
Month(): here
MonthName(): here
[The samples given are 'living' code, if you get errors, your code is to blame]
try this line of code:
MonthName(Month(Now()),1)
a standalone solution:
Dim imonth
Dim mth
imonth = Month(Now()) ' Also tried this.
mth = MonthName(Month(Now()))
WScript.Echo "mth = " & mth
WScript.Echo "mth_short = )" & MonthName(Month(Now()),1)
The issue is that Visual Basic Script is not case sensitive and my variable month was colliding with the built in Month.
As given here and here
Syntax:
Month(Now)
Related
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
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.
I Do have a form with a sub-form (continuous form) on which I would like to apply a date range filter. The thing is that it works partially.
I am using a simple piece of VBA that seem to be THE method. I am using two controls (start & End) and a button to apply the filter.
He is my code sample
With Me.Sub_Desi_Schedule.Form
.Filter = "[Task_End] " & " BETWEEN " & "#" & DateStart & "#" & " AND " & "#" & DateEnd & "#"
.FilterOn = True
And it works fine except when I pick a date range with a value in the last days of the month (as starting period) AND any values within the first week of the next month (as ending period).
Results are blanks even if there is values AND no error message of any kind to help me trouble shooting.
I thought that it could have been something with the date format. I tried to force it to DDMMYYY. No effect and It had the same strange behavior.
And for any other ranges picked later on the month, it works fine...
Does anyone here had this problem before? Is there something obvious I am missing?
I suspect you're right that the problem is due to date format.
Examine the completed filter string the code creates. Do that by using a variable to hold it. Then you can use Debug.Print to see it, and later assign the variable to the form's .Filter property.
Dim strFilter As String
With Me.Sub_Desi_Schedule.Form
strFilter = "[Task_End] BETWEEN #" & DateStart & "# AND #" & DateEnd & "#"
Debug.Print strFilter ' <- view this in Immediate window; Ctrl+g will take you there
.Filter = strFilter
.FilterOn = True
End With
You can avoid problems due to date format by using the unambiguous yyyy-m-d format for those Date/Time values.
strFilter = "[Task_End] BETWEEN " & Format(DateStart, "\#yyyy-m-d\#") & " AND " & Format(DateEnd, "\#yyyy-m-d\#")
I added the # delimiters within the Format expressions. But you don't need to do it that way; I think this should work as well ...
strFilter = "[Task_End] BETWEEN #" & Format(DateStart, "yyyy-m-d") & "# AND #" & Format(DateEnd, "yyyy-m-d") & "#"
Hi there I've created a form on access which looks like this:
I can make it so, that all the filters work, except for the Year and Length filters.
The Year boxes are unbound, and the left is called Year1 and the right one is Year2. I've tried to use Me.Filter code, but it doesn't work. It doesn't come up with an error, it just won't filter my data. So what I need is to make it so that the user can enter a year in Year1 and in Year2, and it filters the data between those two years. So for example they put 2000 in Year1 and 2010 in Year2, and then when they run the query it only shows data from 2000 to 2010.
This is the code I'm currently using:
Private Sub Year2_AfterUpdate()
Me.Filter = "[Year] BETWEEN #" & Me.Year1 & "# AND #" & Me.Year2 & "#"
Me.Filteron = true
Any help would be much appreciated! :)
(If you could explain what code does what that would be much appreciated too, so that I can learn to write it myself, and so that I can understand better! Thanks!)
You only use the "#" characters when you are using a date literal in your filter. If it is just a numeric you would use:
Me.Filter = "[Year] BETWEEN " & Me.Year1 & " AND " & Me.Year2
If [Year] actually IS a date then you should use:
Me.Filter = "[Year] BETWEEN #01/01/" & Me.Year1 & "# AND #12/31/" & Me.Year2 & "#"
Which will evaluate to [Year] BETWEEN #01/01/2010# AND #12/31/2011# if you enter 2010 and 2011 in the year textboxes.
You must use the name of the query (frmAufträgeQuery) and NOT the Form (frmAufträge):
On Error Resume Next
FromDate = "01.01.2021"
ToDate = "01.04.2021"
Me.Filter = "([frmAufträgeQuery].[Administrativ abgeschlossen] Between #" & Format(FromDate, "yyyy-mm-dd") & "# AND #" & Format(ToDate, "yyyy-mm-dd") & "#)"
Me.FilterOn = True
I need to print the date in the format of mm/dd/yyyy.
if the date is 4/24/2009 it should print the date as 04/24/2009.
that is zero padding is also needed..
I used date function to get the current date...but the date is getting in the format of m/dd/yyyy...
Tested in the immediate window and is working for me (output as a comment)
Format(Now, "MM/dd/yyyy") '04/29/2009
Format(Date, "MM/dd/yyyy") '04/29/2009
Format(CStr(Now), "MM/dd/yyyy") '04/29/2009
Format(Date$, "MM/dd/yyyy") '04/29/2009
Format(CDate(Date), "MM/dd/yyyy")'04/29/2009
So whether it is string or datetime should not matter.
Edit: Saw your comment to Fredrik. It doesn't matter how it looks like when you save it to the db table (column date format would be a property of the db and not your program's (or vb's) responsibility). Just format the value as and when you retrieve it from the db.
Note that the "/" character in date formatting functions has a special meaning, as "date separator". This means that i may be replaced with the date separator for the current locale that the program is executed in (here in Sweden it would be replaced with "-" for instance). In order to ensure that you do indeed get the "/" character in the output, I think this would work (I don't have a VB installation to verify with):
Format(date, "MM'/'dd'/'yyyy")
just for the record, escaping the slash will work
Format(dt,"MM\/dd\/yyyy")
Try the next code:
Format(dt,"MM/dd/yyyy")
When you enter date in whatever format it will convert default value so do one thing that in access change your data type date/time to text then it can't affect to your work sure.
I also use VB6 and need to format date in my txt report
this works for me
Format$(Now, "yyyy-mm-dd-00.00.00")
but only if I declare date as string
You can try like this also depending upon your requirement
Dim strDate As String
Dim strDate1() As String
strDate = FormatDateTime(Now, vbGeneralDate)
If InStr(strDate, " ") > 0 Then
strDate1 = Split(strDate, " ")
Dim datDate1 As Date
If Month(strDate1(0)) < 10 Then
txtDate.Text = "0" + strDate1(0)
Else
txtDate.Text = strDate1(0)
End If
Else
End If
Formatting DateTime as a string is straightforward. Often we use format patterns like "HH." But methods like ToShortDateString are also useful.
Example. First we get the current time through DateTime.Now. When you execute these code examples, the current DateTime will be different on your computer.
Here: A format string beginning with MMM (for the month) is used. Look at how the pattern matches up to the output of the program.
VB.NET program that uses format string with DateTime
Module Module1
Sub Main()
' Use current time.
' ... Use a format.
' ... Write to console.
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))
End Sub
End Module
Output
Feb Tue 21 13:26 2017
strDate = Format(strDate, "yyyy-mm-dd")
BillTime = Format(BillTime, "hh:mm:ss")