How to use a "-" in Access DLookup Function Criteria - vba

I'm having trouble using a "-" (dash) in an Access DLookup function criteria.
I have two unbound textboxes called txt_PN and txt_PO_Number. When a user clicks a button, I want to search the db for records where both the part number (txt_PN) and PO number (txt_PO_number) exist in the same record.
The problem I'm having is that if a user enters a part number with a "-" the DLookup function doesn't find the record...even if one exists. DLookup works perfectly for part numbers without dashes.
The code I am using is as follows:
PartCheck = DLookup("Part_Number", "Tbl 01 RDM DB", "Part_Number = '" & Me.txt_PN & "'")
POCheck = DLookup("PO_Number", "Tbl 01 RDM DB", "Part_Number = '" & Me.txt_PN & "'")
If anyone can help me accomodate the use of dashes, it would be very much appreciated.
P.S. The "-" doesn't necessarily appear in the same place from one part number to the next.

You could try to perform a query and check if there are any records.
Modified from this forum:
Dim rsPNCheck As ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT * FROM [Tbl 01 RDM DB] WHERE Part_Number = '" & Me.txt_PN & "' AND PO_Number = '" & Me.txt_PO & "';"
Set rsPNCheck = CurrentProject.Connection.Execute(strSQL)
If Not rsPNCheck.EOF Then
' Notify user record exists
Else
' send email
End If
Set rsPNCheck = Nothing

Ok, I solved the problem...sort of. I'm not sure how the dash problem got resolved, but using the code:
If Not IsNull(DLookup("PO_Number", "Tbl 01 RDM DB", "PO_Number = '" & Me.txt_PO_Number & "' And Part_Number = '" & Me.txt_PN & "'")) Then
[rest of code]
End If
all part numbers are recognized and the program works the way I would like. Thank you all for the suggestions and help.

Related

Update or insert field from table into another table VBA sql

First this is my first post so I apologize for the layout or any other errors with presentation.
I have two tables.
tempWkEndHrs: Is created when the user selects a weekending date and their name from two comboboxs. The result is 5 fields: Name1, Task, Closed Date, Initiated Date and #xx/xx/xxxx# <--the same date as chosen by the user from the combobox to create the table. This is done to limit the number of date fields returned and built into the temp table.
The purpose of this table is so the user can populate as many values under the #xx/xx/xxxx# field/column to show/see the distribution of hours taken on tasks for that week. (its also used as a check to make sure entered values sum up as expected)
I would like to save the new values entered underfield #xx/xx/xxxx# in tempWkEndHrs to the same field #xx/xx/xxxx# in tblHrs.
tblHrs: has fields... Task , #xx/xx/xxxx#, #xx/xx/xxxx#, #xx/xx/xxxx#, #xx/xx/xxxx#, etc.. <--Same dates available within combobox.
I have having difficulty finding an example of updating multiple records from one table to another to fit my situation.
Below is my last attempt from several variations.
Dim strITEM As String
Dim strWkEnd As String
strWkEnd = Me.cmbWkEnd
strSQL = "UPDATE tblHrs SET " & _
"[" & strWkEnd & "] = [" & strWkEnd & "] " & _
"FROM tempWkEndHrs " & _
" WHERE [Task] = [Task]"
*Update 10-24-2017 5:37pm
Dim strITEM As String
Dim strWkEnd As String
strWkEnd = Me.cmbWkEnd
strSQL = "UPDATE tblHrs " & _
"SET tempWkEndHrs.[" & strWkEnd & "] = tblHrs.[" & strWkEnd & "] "& _
"FROM tempWkEndHrs " & _
" WHERE tempWkEndHrs.[Task] = tblHrs.[Task]"
Result:
UPDATE tblHrs SET tempWkEndHrs.[9/30/2017] = tblHrs.[9/30/2017] FROM tempWkEndHrs WHERE tempWkEndHrs.[TASK] = tblHrs.[TASK]
***Run-Time error '3075
***Syntax error(missing operator) in query expression 'tblHrs.[9/30/2017] FROM tempWkEndHrs'.
If this is clear as mud please let me know.
enter image description here
BIG SHOUT OUT TO
Oscar Anthony and Parfait
https://stackoverflow.com/a/34910889/8797058
5.You don't need the FROM tblTemp clause in the SQLReplace String.
6.EDIT: As #Parfait pointed out, tblTemp does not exist in scope of the SQLReplace statement. You should do an INNER JOIN to fix that: <--AWESOME!!
strWkEnd = Me.cmbWkEnd
strSQL = "UPDATE tblHrs INNER JOIN tempWkEndHrs ON tblHrs.TASK = tempWkEndHrs.TASK SET tblHrs.[" & strWkEnd & "] = tempWkEndHrs.[" & strWkEnd & "] "
Works! >:P
I didn't receive any answers to this post so obviously I need to write better questions, I figured it was mud...Thanks for the ~21 Views ?lol.

Run Time error 3061 Too Few parameters. Expected 6. Unable to update table from listbox

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.

Append string to record

Environement : Oracle 11gR2 , ASP .Net, VB
Aim: need to append text string to an existing record.
Problem: When using the following
strSQL += "Update table_name SET "
strSQL += " JOB = '" & Trim(Me.txtjob.Text) & "',"
strSQL += " NAME = '" & Trim(Me.txtname.Text) & "',"
strSQL += " REMARK = REMARK || ' " & Trim(Me.txtremark.Text) & "'"
It appends the already existing data along with the new data to the new data.
Example:
Contents before SQL Execution: ABC
Contents to append: DEF
Result after execution : ABCABCDEF
expected result: ABCDEF
I tried to use a few permutations to get the right result but to no avail.
Any suggestions/resolution will be appreciated.
Okay, it seems that this was a rather straight forward solution which i ended up over complicating.
IN my case:
I was reading the record and displaying it in a text box.
What i ended up doing was :
just update the entire contents of the text box again to the record.
Thus overwriting the already existing contents along with the modified contents of the textbox.
strSQL += " DOC_LOCATION = '" & System.Web.HttpUtility.HtmlEncode(Trim(Me.txtremark.Text)) & (" Last Edit: ") & temp & " " & DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & "'"
Since this was a rather simple application, this solution worked for me.

MS Access - Editing fields after making them autopopulate only edits 1st record (forms)

Just a headsup, I am quite new to relational databases so my question could be a simple fix.
Currently, I have a table with the following data.
ID(1 ,2,3,4,5)
Location(Canada, USA, Japan, Australia, Venezuela)
Count(4,6,2,91,23)
I created a form with a combobox and two text fields. The goal is that I want to be able to click the combobox, have it show all the ID's and when I click an ID, it autopopulates the other two text fields with the corresponding information. After googling a bit, I found a way to do is. Within the Event tab under "on Change" for the combobox, I wrote the these two lines of code.
Me.txtLocation = Me.cboID.Column(1)
Me.txtCount = Me.cboID.Column(2)
However, I also want to be able to edit this information once it has been autopopulated. The problem I'm having is that when I change any of the two textfields, it always edits the first records.
So for example, if I click ID #4, and I change the "Count", it will change the "Count" for the ID #1. Any idea of what I'm doing wrong?
P.S. (I have programming experience but not with VBA)
Thanks in advance!
EDIT:
Private Sub txtCount_AfterUpdate()
Dim strSQL As String
strSQL = "UPDATE aDuh SET Count = '" & Me.Count & "', Location = '" & Me.txtLocation & "' WHERE ID = " & Me.cboID & ""
DoCmd.RunSQL (strSQL)
End Sub
Private Sub txtLocation_AfterUpdate()
Dim strSQL As String
strSQL = "UPDATE aDuh SET Location = '" & Me.txtLocation & "', Count = '" & Me.txtCount & "' WHERE ID = " & Me.cboID & ""
DoCmd.RunSQL (strSQL)
End Sub
Don't bind your form to a table. Put a textbox on your form with it's Visible property set to False. In that textbox, put the value of your Primary Key field (which should be an AutoNumber field) Then when you update your record, pass an UPDATE SQL statement where you update your table based on the value of your Primary Key
Dim strSQL as String
strSQL = "UPDATE aDuh SET Location = '" & Me.txtLocation & "', Count = '" & Me.txtCount & "' WHERE ID = " & Me.cboID & ""
DoCmd.RunSQL (strSQL)

Issues with SQL Query

as may be quiet aparent, i have very little experience with SQL queries.
I'm having problems with the following Query that i'm generating within my Vb.net application
UPDATE Payments SET B1Code = '12345', ARInvoice = '54321', INV2Go = '00000' WHERE PatientID = '400' AND Product = 'Consultation' AND Catagory = 'Orthotics'
(i have created a test record in the database matching the above information)
Its being constructed with the following code in vb.net:
Dim query As String = "UPDATE Payments SET B1Code = '" & txtB1Code.Text & "', ARInvoice = '" & txtARInvoice.Text & "', INV2Go = '" & txtInv2GoCode.Text & "' WHERE PatientID = '" & Integer.Parse(txtID.Text) & "' AND Product = '" & txtProduct.Text & "' AND Catagory = '" & txtPatientType.Text & "'"
Then being passed the my execute query function like this:
DatabaseFunctions.ExecuteQuery(query)
and the function:
Public Shared Sub ExecuteQuery(ByVal SQL As String)
CheckConnection()
Dim cmd As New OdbcCommand(SQL, con)
cmd.ExecuteNonQuery()
End Sub
The function works perfectly, i've used it time and time again for creating/editing records using simple sql queries built in similar ways as above.. The problem is this particular query returns an error:
ERROR [07002ض] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
Maybe someone with more sql experience than me can see what i'm missing ?
Thanks
This error indicates, that one of the columsn you use in your query does not exist.
Check your query again: did you mean Catagory OR Category ?