Can't find what is wrong with the code - ms-access

I Have this code, and I can't figure out what is wrong with it. It does not return any error but field Date_Returned is not getting updated. Please help.
Private Sub txtbxret_Click()
Dim query As String
query = "UPDATE Rent SET Date_Returned = '" & Date & "' WHERE Date_Rent = " & txtrented.Value & " AND Customer_ID = " & txtbxcustID.Value & " AND Movie_ID = " & txtbxmovID.Value
DoCmd.RunSQL (query)
End Sub
I've double and triple checked all the field names and thay are ok by the way...

You must use proper formatting of string expressions of date values in SQL:
query = "UPDATE Rent SET Date_Returned = Date() WHERE Date_Rent = #" & Format(txtrented.Value, "yyyy\/mm\/dd") & "# AND Customer_ID = " & txtbxcustID.Value & " AND Movie_ID = " & txtbxmovID.Value

Related

Why doesn't this DAO recordset query return any results?

I am working on an Access 2007 database and I have set up a DAO recordset query, however, it is not returning any results. It does not throw up any errors, just no results. Me.ProID is a text field, it includes letters and numbers. I think it has something to do with the text field, maybe the placement of quotations.
Dim contractSQL As String
Dim contractDB As DAO.Database
Dim contractRS As DAO.Recordset
contractSQL = "SELECT Top 1 ContractName, ItemDescription, Price FROM ContractPricing WHERE AccountNo = " & Me.Parent.[AccntNumber] & " AND PartNo = " & """ & Me.ProdID & """ & " ORDER BY Price Asc"
Set contractDB = CurrentDb
Set contractRS = contractDB.OpenRecordset(contractSQL)
Debug.Print contractRS.RecordCount
That string concatenation is likely wrong. " & """ & Me.ProdID & """ & " should almost certainly be '" & Me.ProdID & "'
I don't know if you're referring to this:
contractSQL = "SELECT Top 1 ContractName, ItemDescription, Price FROM ContractPricing WHERE AccountNo = " & Me.Parent.[AccntNumber] & " AND PartNo = " & Me.ProdID & " ORDER BY Price Asc"
I'm not sure if this would help you. I'm also new to this. But hopefully it will.

MS Access VB: How do I update a table with a parametrized query ONLY with textboxes that are not empty?

I have some VBA code that will update a table based on a form. It works great, except that I want the table to only update wherever the user fill in info. If that textbox is instead blank, do no update that field in the table. Below is the working update code. The line that starts with If query.Parameters("P1")... is my attempt at trying to make an If statement that will find which textbox values are Null and then ignore those, but I don't know if that's even going in the right direction.
Private Sub Command133_Click()
'Update row for downtime
Dim dbsCurrent As Database
Set dbsCurrent = CurrentDb
', suffix, production_date, reason, downtime_minutes, comment ,'" & CInt(Me.Text118) & "','" & CDate(Me.Text126) & "','" & Me.Text121 & "','" & CDbl(Me.Text123) & "','" & Me.Text128 & "'
'dbsCurrent.Execute " INSERT INTO tbl_Downtime (production_date) SELECT #" & Me.Text126 & "# FROM tbl_Downtime As t WHERE t.ID = " & Me.Text135 & ";"
'dbsCurrent.Execute "UPDATE tbl_Downtime SET job = '" & Me.Text116 & "', suffix = '" & Me.Text118 & "', production_date = #" & Me.Text126 & "#, reason = '" & Me.Text121 & "', downtime_minutes = " & Me.Text123 & ", comment = '" & Me.Text128 & "', shift = '" & Me.Text144 & "' WHERE t.ID = " & Me.Text135 & ";"
Dim query As QueryDef
Dim sql As String
For Each query In CurrentDb.QueryDefs
If query.Name = "UpdateDowntime" Then
Exit For
End If
Next query
If query Is Nothing Then
sql = "parameters " & _
"P1 text, P2 text, P3 Date, P4 Text, P5 Number, P6 Text, P7 Text, P8 Number;" & _
"UPDATE [tbl_Downtime] " & _
"SET job = [P1], suffix = [P2], production_date = [P3], reason = [P4], downtime_minutes = [P5], comment = [P6], shift = [P7] " & _
"WHERE[tbl_Downtime].id = [P8]"
'"(job, suffix, production_date, reason, downtime_minutes, comment, shift) " & _
'" VALUES ([P1], [P2], [P3], [P4], [P5], [P6], [P7]) WHERE[tbl_Downtime].id = [P8]"
Set query = CurrentDb.CreateQueryDef("UpdateDowntime", sql)
End If
query.Parameters("P1").Value = Me.Text116
query.Parameters("P2").Value = Me.Text118
query.Parameters("P3").Value = Me.Text126
query.Parameters("P4").Value = Me.Text121
query.Parameters("P5").Value = Me.Text123
query.Parameters("P6").Value = Me.Text128
query.Parameters("P7").Value = Me.Text144
query.Parameters("P8").Value = Me.Text135
If query.Parameters("P1").Value = "" Then Set query.Parameters("P1").Value = job End If ' WHERE [tbl_Downtime].id = [P8] END
query.Execute
An empty string is not the same as a null value in a database, so you need to change the parameter value to nothing if you want it to equate to null (or default value if one exists).
You could either conditionally set the parameter value like this:
If textbox1.text <> "" then
query.Parameters("P1").Value = textbox1.text
End if
or you could write a function to set the parameter to nothing if the textbox is empty:
Function NothingIfEmpty(value As String)
If value = "" Then
NothingIfEmpty = Nothing
Else
NothingIfEmpty = value
End If
End Function
and use it like this:
query.Parameters("P1").Value = NothingIfEmpty(textbox1.text)
query.Parameters("P2").Value = NothingIfEmpty(textbox2.text)
You can do it this way:
"SET job = Nz([P1], job), suffix = Nz([P2], suffix), production_date = Nz([P3], production_date), reason = Nz([P4], reason), downtime_minutes = Nz([P5], downtime_minutes), comment = Nz([P6], comment), shift = Nz([P7], shift) " & _
"WHERE [tbl_Downtime].id = Nz([P8], -[id])"

How do I update MySQL from visual studio 2010?

I need to update a MySQL database with the new values just collected.
The program will get values like this:
sql = "select * from Accounts where username ='" & txtuname.Text & "' and userpassword = '" & txtpass.Text & "'"
Dim credits As String
credits = publictable.Rows(0).Item(5)
credit.Text = "" & credits
Then it will update the current value with a new value like this:
cmd.CommandText = "UPDATE * Accounts SET credits = '& credit.Text + 10 &' WHERE username = '& Form1.txtuname.Text & '"
However, I am stuck on the UPDATE string.
I think, you have to study well about the update command, since your query syntax is wrong. and you are making some syntactical mistake in the formation of the query. use Line continuation operators to make the query more understandable. plan the output before you query.
cmd.CommandText = "UPDATE Accounts SET " & _
"credits = '" & val(credit.Text) + 10 & "'" & _
"WHERE username = '" & Form1.txtuname.Text & '"

VBA: Syntax error when passing numeric value to a field using UPDATE

Here is my code
Dim orderid As Long
orderid = DMax("Number", "Orders", "") + 1
DoCmd.RunSQL "Update Order_temp " _
& "Set Number = " & orderid & ", Name = '" & Me.Textbox & "' " _
& "WHERE (Name = '*')"
It works fine if it's only like this
Dim orderid As Long
orderid = DMax("Number", "Orders", "") + 1
DoCmd.RunSQL "Update Order_temp " _
& "Set Name = '" & Me.Textbox & "' " _
& "WHERE (Name = '*')"
Thank you in advance.
try using square brackets when referencing fields of a table like so :
DoCmd.RunSQL "Update [Order_temp] " _
& "Set [Number] = " & orderid & ", [Name] = '" & Me.Textbox & "' " _
& "WHERE ([Name] = '*')"
It will help avoiding conflicts on reserved words like Number.
Also I would edit a bit the other line as well :
orderid = Nz(DMax("Number", "Orders", ""),0) + 1
In order to avoid a Null exception.

Dlookup retrieves a date wrong

I'm trying to get a date in my database but when the day is less than 12, the month and the day are switched
Example: In the database 2012-02-10 (2 october 2012), the value I get when I do that:
lastDateMill = Nz(DLookup("LastContactDate", "Mills", "MillID = " & lstMills.Column(0, i)), 0)
is
lastDateMill = "10/02/2012"
So I thought that was only a format thing but when I do
Format(lastDateMill, "Long Date")
it equals "February-10-12"
This is how I update the date
DoCmd.RunSQL "UPDATE Mills SET LastContactDate = #" & SalesCallDate & "# WHERE MillID = " & lstMills.Column(0, i)
And the SalesCallDate = "2/10/2012" so the good date
So why the day and the month are switched?
The front end is ms-access-2010 and the back end is on SQL SERVER 2012
Your SalesCallDate variable contains a date as text:
SalesCallDate = "2/10/2012"
Apparently you intend the date format of that string to be m/d/yyyy, but it gets interpreted as d/m/yyyy format.
Store the string value in yyyy/mm/dd format to eliminate confusion due to locale issues ... 2012/02/10
Since it turns out that SalesCallDate is actually a text box containing a date value, change your UPDATE approach to avoid date problems due to locale.
Dim strUpdate As String
Dim db As DAO.Database
strUpdate = "UPDATE Mills SET LastContactDate = " & _
Format(Me.SalesCallDate, "\#yyyy-m-d\#") & vbCrLf & _
"WHERE MillID = " & Me.lstMills.Column(0, i)
Debug.Print strUpdate
Set db = CurrentDb
db.Execute strUpdate, dbFailonerror
Set db = Nothing
Try this, by explicitly specifying a format
DoCmd.RunSQL "UPDATE Mills SET LastContactDate = #" & _
Format$(SalesCallDate,"yyyy/mm/dd") & "# WHERE MillID = " & _
lstMills.Column(0, i)
UPDATE:
Maybe there is better way to do it, that is independent of any formattings. The idea is to tranfer the date from table to table, without any combo-, list- or text in between. Therefore any conversion from a date type to string and then back to a date field is avoided.
If the tables can be joined (assuming that MillID is the bound field of the listbox):
DoCmd.RunSQL "UPDATE Mills " & _
"INNER JOIN sourceTable ON Mills.MillID = sourceTable.MillID " & _
"SET LastContactDate = sourceTable.SalesCallDate " & _
"WHERE Mills.MillID = " & lstMills
Otherwise
DoCmd.RunSQL "UPDATE Mills SET LastContactDate = " & _
"(SELECT SalesCallDate FROM sourceTable WHERE ID = " & sourceID & ")" _
"WHERE Mills.MillID = " & lstMills