iam using ms access database and i want to get data between two dates
stdate= starting date
edate=end date
iam using the code below.
Private Sub Label3_Click()
DataGrid1.Visible = True
Dim stdate As Date
Dim edate As Date
Format = stdate("mm/dd/yyyy")
Format = edate("mm/dd/yyyy")
stdate = Text1.Text
edate = Text2.Text
Adodc1.RecordSource = "SELECT * FROM sales_info WHERE date BETWEEN &stdate& and &edate& "
Adodc1.Refresh
End Sub
errors iam getting are:
syntax error expected array(in format statement)
missing operator in query expression date.
thanks
plz help
iam a beginner
This code should work-
Private Sub Label3_Click()
DataGrid1.Visible = True
Dim stdate As String
Dim edate As String
stdate = Format(Text1.Text, "mm/dd/yyyy")
edate = Format(Text2.Text, "mm/dd/yyyy")
Adodc1.RecordSource = "SELECT * FROM [sales_info] WHERE [date] BETWEEN #" & stdate & " and " & edate & "]"
Adodc1.Refresh
End Sub
I also agree with #Joe in that you should not use date for your column name as it is typically a reserved word. The [] get around that, but it will make life tougher in the long run.
try to change name of your field because date is a reserved keyword.
After change the format of your date to use a format compliant to VB6 (with #)
The lines Format = stdate("mm/dd/yyyy") and Format = edate("mm/dd/yyyy")
don't look like valid VB6 to me.
You will need to build the query string by gluing the bits together, the interpreter cannot see inside the quotation marks to understand that you want a variable there
"SELECT * FROM sales_info WHERE date BETWEEN #" & stdate & "# and #" & edate &"#"
Try if this works. :)
Adodc1.RecordSource = = "SELECT * " & "FROM tbl " & _
"WHERE Date >= #" & stdate & "# AND Date <= #" & edate & "# "
Adodc1.Requery
Table_1BindingSource.Filter="DateColumnName>=# & DateTimePicker1.text & "# and DateColumName<=#" & DateTimePicker2.text & "#"
Related
I am trying to update my Table (Referral) Field name (referdate) type Date
to be updated by using inputbox, where the user pass the number through the input box and then to this number to referdate to give me date + 2 days result. (example: inputbox 2 days, add 2 days to 20-12-2020 result is 22-12-2020)
the error I get (syntax error in the update statement)
my access version is 2013
my code below:
Dim S As Integer
S = InputBox(" How many days to follow", "Number of Days !")
DoCmd.RunSQL "UPDATE Referral" & _
"SET referdate = referdate" & Me.referdate + S & _
"where SRSno = " & Me.SRSno
Me.Refresh
According to the docs (https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/update-statement-microsoft-access-sql), it seems that your statement might be including the existing column value being concatenated with the local variable.. Try this:
DoCmd.RunSQL "UPDATE Referral" & _
"SET referdate = " & Me.referdate + S & _
"where SRSno = " & Me.SRSno
The difference is subtle, but the Microsoft example shows adding a value as part of the query expression to the existing column value:
SET OrderAmount = OrderAmount * 1.1,
The alternative syntax for your case might be:
DoCmd.RunSQL "UPDATE Referral" & _
"SET referdate = referdate + " & S & _
"where SRSno = " & Me.SRSno
In either case, notice that referdate only appears once in the statement.
Include DateAdd and don't forget the spaces:
If Val(S) > 0 Then
DoCmd.RunSQL "UPDATE Referral " & _
"SET referdate = DateAdd('d', " & Val(S) & ", referdata) " & _
"WHERE SRSno = " & Me.SRSno & ""
End If
when i choose same date from month 10(say dtpckr1 = 02-10-2019 and dtpckr2 = 02-10-2019 ) ..data datagrid does not print anything and shows the msgbox not record found which i code for convinence...but when i choose start date from last moth and end date in this month(say dtpckr1 = 30-09-2019 and dtpckr2 = 02-10-2019 ) it shows all the data from month 09 and nothing from month 10 ... and the strange this is when choose date which is from moth 09 even if it is same(say dtpckr1 = 13-09-2019 and dtpckr2 = 13-09-2019 or 22-09-2019) it works perfectly ..so please try to help me out by refering the following code ..so far i found out that the data which i am getting in datagridview is as per days (dd) not as per whole date...means if i choose the date1 = 31/09/2019 and date2 = 01/10/2019 then it will show the data from date 01 to 31 only from month 09.... I also checked the date format of database and my input,they are same...in databse the date datatype is "date/time" and format is "short date"....if have any other solution then please tell me... i will try... my purpose it to show datewise food orders in datagridview and then calculate the total sale... i am newbie in vb6...so if you can edit my code and repost it ..it will be great...because i want to submit this project by tomorrow..and this is the only which is bothering me... thank you
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub cmdSearch_Click()
Dim date1 As Date
Dim date2 As Date
If IsNull(DTPicker1.Value And DTPicker2.Value) Then
MsgBox "You must select date", vbCritical, "Warning"
Exit Sub
End If
DTPicker1.Value = Format(DTPicker1.Value, "dd-mm-yyyy")
DTPicker2.Value = Format(DTPicker2.Value, "dd-mm-yyyy")
date1 = DTPicker1.Value
date2 = DTPicker2.Value
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\OrderMania\ordermania.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient
If DTPicker2.Value < DTPicker1.Value Then
MsgBox "End Date Cannot Be Lesser Then Start Date", vbCritical, "Wrong Input"
Exit Sub
Else
Adodc1.RecordSource = "select * from order1 where (date between #" & date1 & "# and #" & DTPicker2.Value & "#)"
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
MsgBox "Please Enter Another Date", vbCritical, "No Record Found"
Else
Adodc1.Caption = Adodc1.RecordSource
End If
End If
con.Close
Call sale
End Sub
Public Sub sale()
Dim i As Integer
Dim Tot, gst, gtot As Double
For i = 0 To Adodc1.Recordset.RecordCount - 1
Tot = Tot + CDbl(DataGrid1.Columns(5).Text)
Adodc1.Recordset.MoveNext
Next i
Text1.Text = Tot
gst = Tot * 0.05
Text2.Text = gst
gtot = Tot + gst
Text3.Text = gtot
End Sub
Try inverting month and day in your between clause :
..."between #" & Format(date1, "mm-dd-yyyy") & "# and #" & Format(date2, "mm-dd-yyyy")) & "#)"
But concatenation of SQL string with variables values is considered bad practice, as #GSerg remind me, since SQL injection of malicious code could occurs. You should work with parameters. If you want to study this, here is a start point : https://learn.microsoft.com/fr-fr/office/client-developer/access/desktop-database-reference/createparameter-method-ado
I do a function to assign an ID. But when I click button, this error comes out.
Microsoft VBScript runtime error '800a000d' Type mismatch: 'LastID'
Public function AssignSanctionID(DeptID,SectID,SanctionType)
REM obtain Transaction ID
dim CmdX
dim SQLX
dim RsX
dim Prefix
dim LastID
dim CurrID
dim NewCurrID
'- Set Connection
HariNi=now()
Tahun=year(HariNi)
Bulan=month(HariNi)
if len(bulan)=1 then
Bulan= "0" & Bulan
end if
If Cint(Tahun) < 2016 then
Pref1= DeptID & "/" & SectID & "/"
Prefix=DeptID & "/" & SectID & "/" & Tahun & "/" & Bulan & "/"
else
Pref1= DeptID & "/%/" & SectID
Prefix=DeptID & "/" & Tahun & "/" & Bulan & "/"
end if
set CmdX = server.CreateObject("ADODB.Command")
Set RSX = Server.CreateObject("ADODB.Recordset")
SQLX = " SELECT * FROM Sanction " _
& " WHERE SanctionID like '%" & Pref1 & "%' " _
& " ORDER BY ID DESC"
CmdX.ActiveConnection = objconn
CmdX.CommandText = SQLX
RsX.Open CmdX,,0,1
if not(RsX.BOF and RsX.EOF) then
If Cint(Tahun) < 2016 then
LastID = right(RsX("ID"),4)
else
LastID = mid(RsX("ID"),13,4)
end if
else
if Bulan="04" then
LastID=0
end if
end if
RsX.Close
set RsX = nothing
'Set ID
If LastID<>"" then
'CurrID = left(4)
CurrID=int(LastID)+1
end if
if len(currid)>0 then
select case len(currid)
case 1
newcurrid = "000" & currid
case 2
newcurrid = "00" & currid
case 3
newcurrid = "0" & currid
case 4
newcurrid = currid
end select
else
NewCurrID="0001"
end if
If Cint(Tahun) < 2016 then
NewCurrID=Prefix & NewCurrID
else
NewCurrID=Prefix & NewCurrID & "/" & SectID
end if
AssignSanctionID = NewCurrID
end function
Hard to help if I don't see the data.
From quick view of the code the issue is here:
CurrID=int(LastID)+1
You are trying to cast LastID but are you sure that it is convertible? Could list all possible values?
Short answer: CInt only works with Numerical values. If you have letters in your value, then Cint wont work.
Bit longer answer:
Having read the Blog that we should be more welcoming (https://stackoverflow.blog/2018/04/26/stack-overflow-isnt-very-welcoming-its-time-for-that-to-change/?cb=1), here is a very general answer, but that might lead you on the correct way to fix it yourself.
Type Mismatch is an error you can get when using a variable the wrong way. For example if you try to do numerical functions with Strings (which means the variable contains letters a-z etc) you will get "Type Mismatch" as you cant add or subtract text in a mathematical way... On the other hand you cant add Integer variables (the variable only contains a number AND isnt contained within "quote marks").
So below is a few ways to assigna a variable and what type it becomes:
LastID=1 'This makes LastID an INT (number)
LastID="1" 'This makes LastID a String but a CInt(LastID) can turn it into an INT because it ONLY contains numbers.
LastID="IT" 'This makes LastID a String that CANT in any way be cast to INT as it contains letters.
LastID=IT 'This row will either create an error except if you already have a variable called IT, then LastID will get the same value as the IT variable...
This should hopefully get you on your way to fix this issue...
This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 4 years ago.
My issue is when I retrieve date back it gives me in this format:
lastseenstatus=rsprefobj("lastseentstamp")
19-07-2014 15:31:32
I want it in 7/19/2014 3:31:32 PM format with AM/PM intact.
Please help..
First and foremost you need to determine the data type of rsprefobj("lastseentstamp"):
MsgBox TypeName(rsprefobj("lastseentstamp"))
If it's a string, you need to convert it to a datetime value first:
lastseenstatus = CDate(rsprefobj("lastseentstamp"))
If you want the date formatted according to the system's regional settings, use the FormatDateTime() function as #John suggested:
MsgBox FormatDateTime(lastseenstatus)
If you need a distinct date format regardless of the system's regional settings you have to either build the formatted string yourself:
Function LPad(v) : LPad = Right("00" & v, 2) : End Function
Function FormatDate(d)
formattedDate = Month(d) & "/" & LPad(Day(d)) & "/" & Year(d) & " " & _
((Hour(d) + 23) Mod 12 + 1) & ":" & LPad(Minute(d)) & ":" & _
LPad(Second(d))
If Hour(d) < 12 Then
formattedDate = formattedDate & " AM"
Else
formattedDate = formattedDate & " PM"
End If
FormatDate = formattedDate
End Function
MsgBox FormatDate(lastseenstatus)
or use the .Net StringBuilder class:
Set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat "{0:M\/dd\/yyyy h:mm:ss tt}", lastseenstatus
MsgBox sb.ToString()
In my tests I wasn't able to get the tt format specifier to work, though, so you may have to resort to something like this:
Set sb = CreateObject("System.Text.StringBuilder")
If Hour(lastseenstatus) < 12 Then
am_pm = "AM"
Else
am_pm = "PM"
End If
sb.AppendFormat_5 Nothing, "{0:M\/dd\/yyyy h:mm:ss} {1}", _
Array(lastseenstatus, am_pm)
MsgBox sb.ToString()
I'm assuming you are using VBScript and not VB.NET like you have tagged.
Use FormatDateTime(lastseenstatus).
That should give you the format "2/16/2010 1:45:00 PM".
I have a vb6 project and i need to update a visual foxpro table from a recordset.My issue is when i try to update the table i get error msg:Command contains unregnized phrase/keywords.My problem is situated where the date field is concern.I dont know if i written the last portion of the code right.Here is my code:
rs2.Open "update transac set no_ot_1_5 = " & rs1.Fields("ovt1") & ", no_ot_2_0 = " & rs1.Fields("ovt2") & ", no_ot_3_0" _
& "= " & rs1.Fields("ovt3") & "where code = '" & rs1.Fields("emp_code") & "and transac.date = & trans.txtend &", cn1, adOpenDynamic, adLockPessimistic
Try this:
rs2.Open "update transac set no_ot_1_5 = " & rs1.Fields("ovt1") & ", no_ot_2_0 = " & rs1.Fields("ovt2") & ", no_ot_3_0" _
& "= " & rs1.Fields("ovt3") & " where code = '" & rs1.Fields("emp_code") & "' and transac.date = '" & trans.txtend &"'", cn1, adOpenDynamic, adLockPessimistic
It looks like you were missing a space before the WHERE keyword and you missed a single-quote after emp_code. It also looks like you had a problem with transac date.
If the solution from G Mastros is still not complete, it may be due to incorrect data type of a "Date" field. you may need to change to
transac.date = CTOD('" & trans.txtend &"') "
as if you are sending in a text string, but the date is of a DATE type field, you'll need to have it converted to a VFP recognized function... CTOD() is Convert Character String to a Date.