I'm trying to do this update to my SQL table but no luck so far.
The datetime in my SQL is: mm/dd/yyyy hh:mm:ss AM/PM.
When I use now(), the format is dd/mm/yyyy hh:mm:ss AM/PM.
So I change it to match:
new_now = month(now())&"/"&day(now())&"/"&year(now())&" "&time()
and here is my update SQL:
"UPDATE tblpo_po set purchase_status = '2', purchased_at = "&new_now&" where po_id = '"&request("po_number")&"' "
No luck so far.
error:
Incorrect_syntax_near_'10' (which is not relevant at all anyway)
Any idea?
found it, here is how: (http://p2p.wrox.com/classic-asp-basics/32475-asp-sql-server-datetime-field.html)
FUNCTION amDate(varDate)
IF isNull(varDate) OR Trim(varDate) = "" OR varDate = "Null" THEN
amDate = "Null"
ELSE
amDate = "'" & Month(DateValue(varDate)) & "/" & Day(DateValue(varDate)) & "/" & Year(DateValue(varDate)) & " " & TimeValue(varDate) & "'"
END IF
END FUNCTION
Try this (single quotes not neccessary, the function puts these in there, its a good practice to always use triling semi colons):
"Update LOGS SET LastActionBy = " & amDate(NOW()) & " WHERE ID = 5;"
then it works.
ref: http://p2p.wrox.com/classic-asp-basics/32475-asp-sql-server-datetime-field.html
Just need single quotes around your original date value. The "10" error was probably the hour of the time.
I would also suggest using parameters instead of sql string concatenation.
Related
SQL database with dates. I have a list form. At the top of the form, I am trying to filter the data by Date Received. When it brings it into Access, DateReceived shows in yyyy-MM-dd format so I have the SQL data invisible and I have a formatted text box (txtDateReceived) which takes the SQL date and formats it into mm/dd/yyyy which is visible. The user will enter the date in the mm/dd/yyyy format OR pick from the calendar. I tried to apply the filter to DateReceived and to txtDateReceived, and can't get either to work. Here is my code (includes everything I tried):
Private Sub TextDateR_AfterUpdate()
'declare variables
Dim sFilter As String
'in this case, the ID is text so the ID value
'needs to be wrapped in single quotes.
sFilter = "[DateReceived]= " & Format(Me.TextDateR, "yyyy-MM-dd")
'sFilter = "DateReceived = #" & Me.TextDateR & "#"
'sFilter = "txtDateReceived =" & Me.TextDateR
'assign the filter value,and turn filtering on
Me.Filter = sFilter
Me.FilterOn = True
Me.Recalc
Forms!frmSurplusList.Requery
Forms!frmSurplusList.Repaint
sFilter = ""
End Sub
Figured it out! sFilter = "[DateReceived]= '" & Format(Me.TextDateR, "yyyy-MM-dd") & "'"
You should create not a text date (that will be casted) but a date value expression using octothorpes:
sFilter = "[DateReceived] = #" & Format(Me.TextDateR, "yyyy-MM-dd") & "#"
Morning all,
I am hoping this is a quick one. I have a query in Excel that requests records between two date points. It returns without errors but also returns unexpected values, seemingly because the date has been swapped around. I am working with the UK date format (dd/mm/yyy).
I have tried passing the date as a date type and a string type, as a datevalue and just the text. I have also tried passing it as yyyymmdd.
In the below example sFromDate is 06/04/2016 (6th April) and sToDate is 12/04/16 (12th April). Dates between June and December are being returned.
sSelectedArea = "ThisTMRacf"
sFromDate = DateValue(Sht_SpecificView.Range("D5"))
sToDate = DateValue(Sht_SpecificView.Range("F5"))
Set rex = db.OpenRecordset("SELECT eGain.RecordDate FROM [eGain], [Staff]
WHERE eGain.Racf = Staff.Racf AND (Staff.TeamManager = '" & sSelectedArea & "'
OR Staff.CSM = '" & sSelectedArea & "')
AND eGain.RecordDate BETWEEN #" & sFromDate & "# AND #" & sToDate & "# ;")
Is there another way to pass the date so that it doesn't convert?
Formatting the dates as yyyy/mm/dd worked. Example below.
sFromDate = Format(Sht_Dashboard.Range("D5"), "yyyy/mm/dd")
sToDate = Format(Sht_Dashboard.Range("F5"), "yyyy/mm/dd")
Might have to be ruthless with this and break the dates down and rebuild...
sFromDateY = Year(DateValue(Sht_SpecificView.Range("D5")))
sFromDateM = Month(DateValue(Sht_SpecificView.Range("D5")))
sFromDateD = Day(DateValue(Sht_SpecificView.Range("D5")))
Then use
AND eGain.RecordDate BETWEEN DateSerial(" & sFromDateY &", "& sFromDateM &", "&sFromDateD &") ...
I am trying to use an update statement to update a certain record, however I am having difficulty. I would like to replace ‘user’ and ‘timestamp’ with variable values however When I provide inline variables I receive an error, can someone help me to achieve this functionality. I an using sql server as my dbms.
Variables:
Dim timeStamp As String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
Dim user As String = GetUserName()
Update Statement:
strsql = strsql & " Update Activity Set userName = 'user', timeDate = 'timeStamp' Where noteKey = " & lngNoteKey
You will want to use parameterized queries to do this.
strsql = strsql & " Update Activity Set userName = #user, timeDate = #date Where noteKey = " & lngNoteKey
and then add the parameters to the command when you execute it:
yourQuery.Parameters.Add(new ObjectParameter("user", user));
yourQuery.Parameters.Add(new ObjectParameter("date", timeStamp));
strsql = strsql & " Update Activity Set userName = '" & user & "', timeDate = '" & timeStamp & "' Where noteKey = " & lngNoteKey
Try that. concatenate the data into the string.
All,
I am running the below SQL and I keep getting error 3061. Thank you all for the wonderful help! I've been trying to teach myself and I am 10 days in and oh my I am in for a treat!
Private Sub b_Update_Click()
Dim db As DAO.Database
Set db = CurrentDb
strSQL = "UPDATE Main" _
& " SET t_Name = Me.txt_Name, t_Date = Me.txt_Date, t_ContactID = Me.txt_Contact, t_Score = Me.txt_Score, t_Comments = Me.txt_Comments" _
& " WHERE RecordID = Me.lbl_RecordID.Caption"
CurrentDb.Execute strSQL
I am not sure but, you can try somethink like that
if you knom the new value to insert in the database try with a syntax like this one
UPDATE table
SET Users.name = 'NewName',
Users.address = 'MyNewAdresse'
WHERE Users.id_User = 10;
Now, if you want to use a form (php)
You have to use this
if(isset($_REQUEST["id_user" ])) {$id_user = $_REQUEST["id_user" ];}
else {$id_user = "" ;}
if(isset($_REQUEST["name" ])) {$name= $_REQUEST["name" ];}
else {$name = "" ;}
if(isset($_REQUEST["address" ])) {$address= $_REQUEST["adress" ];}
else {$adress= "" ;}
if you use mysql
UPDATE table
SET Users.name = '$name',
Users.address = '$adress'
WHERE Users.id_User = 10;
i don't know VBA but I will try to help you
Going on from my comment, you first need to declare strSQL as a string variable.
Where your error expects 6 values and access doesn't know what they are. This is because form objects need to be outside the quotations of the SQL query, otherwise (as in this case) it will think they are variables and obviously undefined. The 6 expected are the 5 form fields plus 'strSQL'.
Private Sub b_Update_Click()
Dim db As DAO.Database
dim strSQL as string
Set db = CurrentDb
strSQL = "UPDATE Main" & _
" SET t_Name = '" & Me.txt_Name & "'," & _
" t_Date =#" & Me.txt_Date & "#," & _
" t_ContactID =" & Me.txt_Contact & "," & _
" t_Score =" & Me.txt_Score & "," & _
" t_Comments = '" & Me.txt_Comments & "'," & _
" WHERE RecordID = '" & Me.lbl_RecordID.Caption & "';"
CurrentDb.Execute strSQL
end sub
Note how I have used double quotes to put the form fields outside of the query string so access knows they aren't variables.
If your field is a string, it needs encapsulating in single quotes like so 'string'. If you have a date field it needs encapsulating in number signs like so #date# and numbers/integers don't need encapsulating.
Look at the code I have done and you can see I have used these single quotes and number signs to encapsulate certain fields. I guessed based on the names of the fields like ID's as numbers. I may have got some wrong so alter where applicable... Or comment and I will correct my answer.
I'm trying to query the SQL database for all lines where the date is after a date given through user input. I've run into a variety of errors from "incorrect syntax near" when I surround my date with "#" to "arithmetic overflow error converting expression to". My current code looks like this:
inputdate = InputBox("Please enter the starting date (mm/dd/yyyy)")
Debug.Print inputdate
querydate = "(DTG > " & Format(inputdate, "MMDDYYYY") & ")"
select StationID, DTG, CeilingFeet from SurfaceOb where " & querydate & " and StationID = 'NZWD'"
DTG is the column name for the date-time group in the SQL database. Any idea where I am going wrong? I've tried every solution I could find over the past few days without luck. Thank you in advance.
The primary issue is that dates must be enclosed in single-quotes. Here is a complete working example (minus a valid connection string) that should explain how to achieve your objective. Note that you will also want to switch to the ISO date format, in which the order is Year-Month-Day (YYYY-MM-DD).
Sub UpdateRecords()
Dim connection As New ADODB.connection
Dim recordset As New ADODB.recordset
Dim connectionString As String
Dim query As String
Dim inputdate As Date
Dim querydate As String
inputdate = InputBox("Please enter the starting date (mm/dd/yyyy)")
querydate = "(DTG > '" & Format(inputdate, "yyyy-mm-dd") & "')"
query = "select StationID, DTG, CeilingFeet from SurfaceOb where " & querydate & " and StationID = 'NZWD'"
connectionString = "..."
connection.Open connectionString
recordset.Open query, connection
ActiveSheet.Range("A1").CopyFromRecordset recordset
End Sub
Dates for SQL Server should be formatted as a date or date/time, qualified with single-quotes:
Date in ISO unseparated date format
querydate = "(DTG > '" & Format(inputdate, "yyyymmdd") & "')"
Date/Time in ISO 8601 format
querydate = "(DTG > '" & Format(inputdate, "yyyy-mm-ddThh:mm:ss.000") & "')"
Date format for MySQL is YYYY-MM-DD.
Also, as DTG is datetime, you'll need DATE(DTG) > DATE(NOW()) for example - DATE() is a MySQL function that checks only the date portion of a datetime stamp.
Your query should look like this:
querydate = "(DATE(DTG) > " & Format(userinput, "YYYY-MM-DD") & ")"
select StationID, DTG, CeilingFeet from SurfaceOb where " & querydate & " and StationID = 'NZWD'"