set rs6= objconn.execute("select TotalDays from newbank where " & _
"newbank.empid= '" & session("EmpID") & "' and newbank.LeaveType = 23")
Here 23 is the value in the Field named LeaveType, but TotalDays are not being retrieved. Can you help to solve this?
Thank You
save sql and variables to a string first
EmpID = session("EmpID")
sql = "select TotalDays from newbank where empid= '" & EmpID & "' and LeaveType = 23"
log(EmpID )
log(sql)
Must EmpID be a string ?
log is your debugging or logging routine you unbdoubtly have..
session and request variables are best placed in a stringvariable first so you're sure of the type and can inspect the contents
like suggested above test the contents op the sql var againt your database, if it works there perhaps something is wrong with your connectionsetup
Related
I am facing this issue with my query. I would like you to kindly help me resolve it.
MD = "UPDATE librarysystem.audit set timeout = '" & Today + "" + TimeOfDay & "' AND status='0' WHERE username = '" & AccountId & "'AND status = '1'"
cmd = New MySqlCommand(MD, con)
cmd.ExecuteNonQuery()
here's the code:
connect()
Dim result As Integer = MessageBox.Show("Are You Sure You Want To LOGOUT?", "Are You?", MessageBoxButtons.YesNo)
If result = DialogResult.No Then
Me.Show()
ElseIf result = DialogResult.Yes Then
connect()
Dim time As DateTime
time = Date.Today
Dim a As Integer = 0
MD = "UPDATE librarysystem.audit set timeout = '" & Today + "" + TimeOfDay & "' AND status='0' WHERE username = '" & AccountId & "'AND status = '1'"
cmd = New MySqlCommand(MD, con)
cmd.ExecuteNonQuery()
AccountSettings.Hide()
BorrowedBooks.Hide()
LogHistory.Hide()
Login.Hide()
ReturnedBooks.Hide()
SearchBooks.Hide()
End If
End Sub
:
Thank you in advance ^^
One of the deep hassles of SQL is that you're usually embedding one language in another. That makes it hard to read things in both languages clearly. Here's your SQL extracted from your vb.
UPDATE librarysystem.audit
set timeout = '" & Today + "" + TimeOfDay & "'
AND status='0'
WHERE username = '" & AccountId & "'AND status = '1'"
Here it is with some sample values substituted
UPDATE librarysystem.audit
set timeout = '2016-11-26 13:14:15'
AND status='0'
WHERE username = 'SomeUserName'AND status = '1'
You should be able to carry out that statement directly on your dbms and have it function correctly. But, look it over. What do you see?
I see an AND where there should be a , in the list of columns to update. AND only works in WHERE and ON clauses, not in lists of columns.
I see a missing space in the SQL in the sequence 'SomeUserName'AND.
I also see some potential confusion in the way your vb program created the value for timeout. It's not immediately clear whether your vb TimeOfDay variable will render in 24h format (13:14:15) or in am/pm format (01:14:15 PM).
Finally, I see an extraordinarily common mistake. The SQL statement is jammed onto one line, as if it had been written in the 1980s by a psychotic APL programmer. This makes your language-in-a-language program almost impossible to read.
I'm trying to get an access userform button to run an update query.
The parameters I have are:
Invoice number (should update Current ICB.Invoice number)
Material Code (should update Current ICB.Material Code)
Amount USD (Should update current ICB.Amount USD)
Username (Should update current ICB.Owner)
Vendor Code (Should update current ICB.Vendor code)
Record Number (Should = current ICB.ID)
I want to use a button on the form to check for a record that has the same record number and update the fields listed above with the values entered into the userform. I would use an update query but writing SQL into VBA is not my strong point. Any ideas?
In the OnClick event of the button, do something like this:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from MyTable where RecordNumber = " & ICB.ID & "")
rec.Edit
rec("InvoiceNumber") = ICB.Invoice_number
rec("MaterialCode") = ICB.Material_Code
etc...
rec.update
rec.close
Use your actual table and field names, but that's the general idea.
If you absolutely insist on using a query, then you need to write the SQL in code in a similar way and use DoCmd.RunSQL. Something like this:
txtSQL = "UPDATE MyTable SET InvoiceNumber = " & ICB.Invoice_number & ", " & _
" MaterialCode = " & ICB.Material_Code & "" & _
etc...
" WHERE RecordNumber = " & ICB.ID & ""
DoCmd.RunSQL txtSQL
This assumes you're only using integers. If you're storing the data as text, it needs to be surrounded by single quotes.
" MaterialCode = '" & ICB.Material_Code & "'" & _
I need some help. I use a combo box to create a sql query. Here is the query string
SQlstr = "UPDATE maindata SET " _
& "?theoption1 = ?therec1 " _
& "WHERE acct = ?theaccount AND clientnumber=?theclient;"
?theoption1 is a field name but it comes out with an apostrophe like 'xxxxx' , and this causes an error. How do I get around this. I cant use Replace because I am using parameters.
Thanks for any help I can get.
We need to update a table with the users id (NBK). The table with NBK also has the user status (0 - 1) and only one user will have 1 at a time. The challenage is to #1 capture the active user #2 update the other table with the user NBK. I hope the code below just has a simple syntex error that I cannot find?
Dim nb As String
Dim NBK As String
nb = [Employees]![NBK] & "' WHERE "
nb = nb & " " & [Employees]![Status] = '1'
NBK = " Update tbl_DateTracking SET NBK = "
NBK = NBK & "'" & nb & "' WHERE "
NBK = NBK & "CaseId = '" & CaseId & "' AND OCC_Scenario = '" & OCC_Scenario & "' ;"
DoCmd.RunSQL nb
DoCmd.RunSQL NBK
Several pointers of note here:
You should use parameterized queries instead of string concatenation. This prevents/contains SQL injections and other issues regarding malformed input.
Ideally, you should normalize the database so that "active user" is not a field on the user table. What happens when there are two users set as "active" in the database?
With this given schema however, you're going to want to use a sub-select query. Ex:
UPDATE tbl_DateTracking SET NBK=(SELECT NBK FROM Employees WHERE Status=1 LIMIT 1) WHERE CaseID=? AND OOC_Scenario=? and then pass in CaseId and OOC_Scenario as the parameters.
Note, I'm not familiar with VB or how it interacts with SQL; The above is just an example, you'll have to apply it to your application and alter it to make it work. The way you're building and running the nested queries also looks like it won't work, since your first query doesn't contain a command (You probably want SELECT, I think. Does VB do that automatically with []![] syntax?), and when you nest it inside the second query, it's not surrounded with (). Running the first query by itself also has no effect if you're including it as a sub-query in the second one.
I have a simple access form with an unbound textbox that is used to search for records with a matching date, with the results displayed in a subform. The backend database is stored in an SQL Express 2008 instance, accessed via an ODBC connection (SQL Server Native Client 10.0). The client is Access 2007, running on 64 bit Windows 7 professional.
When the user clicks on the search button on the form, the following VBA code is executed to update the subform.
Private Sub UpdateSfmQuery()
Dim query As String
If IsDate(txtDate.Value) And IsNumeric(cboStaff.Value) Then
query = "SELECT * FROM TimesheetEntries " & _
"WHERE StaffId = " & cboStaff.Value & " " & _
"AND [Date] = '" & _
Format(txtDate.Value, "YYYY/MM/DD HH:MM:SS") & "' "
Else
query = "SELECT * FROM TimesheetEntries WHERE 0 = 1;"
End If
frmTimesheetUpdateSfm.Form.RecordSource = query '<--- Fails here
End Sub
The datatype in the SQL Server back end [Date] field is DateTime, and the code above fails on the highlighted line above with the error, Data type mismatch in criteria expression.
I've tried changing the format property of txtDate from short date to general date, and I've also modified the date format in the line above that formats txtDate.Value.
The SQL query contained in the variable query in the above code excerpt (value shown below), executes perfectly when copied and pasted into SQL Express Management Studio.
SELECT * FROM TimesheetEntries WHERE StaffId = 14 AND [Date] = '2011/11/22 00:00:00'
What am I doing wrong?
The problem is that date values inside native Access queries need to be surrounded by pound/hash signs instead of quotes. Basically, like any SQL database, Access has it's own dialect of SQL and it is slightly different from SQL Server.
Private Sub UpdateSfmQuery()
Dim query As String
If IsDate(txtDate.Value) And IsNumeric(cboStaff.Value) Then
query = "SELECT * FROM TimesheetEntries " & _
"WHERE StaffId = " & cboStaff.Value & " " & _
"AND [Date] = #" & _
Format(txtDate.Value, "YYYY/MM/DD HH:MM:SS") & "# "
Else
query = "SELECT * FROM TimesheetEntries WHERE 0 = 1;"
End If
frmTimesheetUpdateSfm.Form.RecordSource = query '<--- Used to fail here
End Sub
Remember, you can always take a mockup of your query and use Access's query editor to try to make the query work.
As a side note, I recommend you tag questions like this with the ms-access tag unless your problem is specific to a certain version of Access.