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

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)

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.

VBA - Access - updating table with new data from user form

I have created a form in Access (provides information on reviewers), of which one part is a comment section (I have two comment sections, one shows the comments against the person and the other is to add new comments).
I have a table that holds the comments against the ID of each person which the form pulls in when you search for a reviewer through a combo box.
I have done the code for adding the comments (ID of the person and the new comments), but it just creates a new field in the table (or does not add it if I stop duplications).
What I want it to do is look to see if the ID is in the table, and if it is, replace the old comment with the new comment, otherwise, add the new ID/Comment to the table.
My code so far is:
Dim strSQL As String
strSQL = "INSERT INTO panelComment (ID, Comments) VALUES (" & Me!Text14 & ", '" & Me!Text29 & "');"
DoCmd.RunSQL strSQL
End Sub
Any suggestions? Thank you.
Dim strSQL As String
If DCount("Id","panelCOmment","ID=" & Me!Text14) >0 then
strSQL = "Update panelComment set Comments = '" & Me!Text29 & "' where Id = " & Me!Text14
else
strSQL = "INSERT INTO panelComment (ID, Comments) VALUES (" & Me!Text14 & ", '" & Me!Text29 & "');"
ENd if
DoCmd.RunSQL strSQL

Ms Access VBA - Trying to call data from a combo box but only if a field is empty

From the code below I want [KronosNO] to come from a database Combo box
but only if the [RTWDate] column is Null.
Set Rs = Application.CurrentDb.OpenRecordset(
"SELECT * FROM tblAbsence WHERE [KronosNo] = '" & Me.cmbKronosNo.Value & "'
AND [RTWDate] = '" & "Is Null" & "' ", dbOpenDynaset)
Don't treat Is Null like a variable, it is a part of SQL.
You simply need
AND [RTWDate] Is Null
in your string.

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.

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

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.