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
Related
I've created a form to add in new performances to a table in access, and a way to view previous performances. However, when I go to view the table, the new performances aren't there. The view table is an inner join, whereas I'm adding the data into the 2 separate tables, is this why I can't view the update?
View Table:
Access.ExecQuery("SELECT ConcertMusic.ClubName, DatePerformed, Music.Piece, Artist FROM Music INNER JOIN ConcertMusic ON ConcertMusic.Piece=Music.Piece;")
If Not String.IsNullOrEmpty(Access.Exception) Then MsgBox(Access.Exception) : Exit Sub
DgvData.DataSource = Access.DBDT
Adding into first table with primary key
Dim Piece As String = TxtPiece.Text
Dim Artist As String = TxtArtist.Text
Access.ExecQuery("INSERT INTO Music(Piece, Artist) VALUES ('" & Piece & "','" & Artist & "');")
If Not String.IsNullOrEmpty(Access.Exception) Then MsgBox(Access.Exception) : Exit Sub
Adding into second table:
Dim Club As String = CmbClub.Text
Dim Term As String = CmbTerm.Text
Dim Dates As String = TxtDate.Text
Dim Piece As String = TxtPiece.Text
Dim ConcertID As String = (Term.Substring(0, 1) & Dates & Club.Substring(0, 2))
Access.ExecQuery("INSERT INTO ConcertMusic ( ConcertID, Term, ClubName, DatePerformed) VALUES ('" & ConcertID & "','" & Term & "','" & Club & "','" & Dates & "');")
I know there are probably better ways to do this, and I've left the database vulnerable to SQL injection but this is for my a level coursework due in on Monday, so Id rather it be a working shell than perfect.
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.
i create a list box that show table records and a delete button in access form
i want when user select an item from list box and click on delete button
with SQL code delete related item that selected in list box
i find some code like this:
Dim strSQL As String
strSQL = "DELETE FROM Tbl_DCC WHERE Text_AgnetName = " & Me.List_DCC.Column(0) &" and "& Me.List_DCC.Column(2)
CurrentDb.Execute strSQL
Me.List_DCC.Requery
But it Does not work
I am thankful with your guidance
try to put single quotes in the where clause
Dim strSQL As String
strSQL = "DELETE FROM Tbl_DCC WHERE Text_AgnetName = '" & Me.List_DCC.Column(0) &"' and '"& Me.List_DCC.Column(2) & "'"
CurrentDb.Execute strSQL
Me.List_DCC.Requery
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)
I have the following coding for a button. My problem is that the Query "SQLStory" is comming up with an error that it is missing a semi colon.
The combobox contains the item name and is ordered by the product ID and the SQLStory is supposed to move all items from the TblTotalSale to the table TblSaleStore. Any Ideas where the error is?
Private Sub StockOK_Click()
Dim SQLDelete1 As String
Dim SQLDelete2 As String
Dim SQLUpdate As String
Dim SQLStory As String
SQLDelete1 = "DELETE * FROM TblStock WHERE TblStock.ProductID = " & CboStockItem.Value
SQLDelete2 = "DELETE * FROM TblTotalSale WHERE TblTotalSale.ProductID = " & CboStockItem.Value
SQLUpdate = "INSERT INTO TblStock (ProductID, StockLevel) VALUES ( " & Me.CboStockItem.Value & "," & Me.TxtStockValue & " )"
SQLStory = "INSERT INTO TblSaleStore (ProductID) VALUES (TblTotalSale.ProductID) FROM TblTotalSale WHERE TblTotalSale.ProductID = " & Me.CboStockItem.Value
If IsNull(Me.TxtStockValue) Then MsgBox "Please Select An Item To Update Stock And Ensure A Value Has Been Entered" Else:
DoCmd.RunSQL SQLDelete1
DoCmd.SetWarnings False
DoCmd.RunSQL SQLStory
DoCmd.RunSQL SQLDelete2
DoCmd.RunSQL SQLUpdate
DoCmd.SetWarnings True
End Sub
Another problem I am having with this code is that the block of doCmd was happening whether the txt box TxtStockValue was null or not, and I only want them to happen if the box is not null... Any Ideas on that part either?
Thanks
Sam
Values are for just that, values such as 'abc' or 123, you need SELECT:
SQLStory = "INSERT INTO TblSaleStore (ProductID) " _
& "SELECT (TblTotalSale.ProductID) FROM " _
& "TblTotalSale WHERE TblTotalSale.ProductID = " _
& Me.CboStockItem.Value
But the above is odd, because you already have the ID in the combo, so, as I said in your previous post on the topic:
SQLStory = "INSERT INTO TblSaleStore (ProductID) " _
& "VALUES ( " & Me.CboStockItem.Value & " )"
Also, it was suggested to you that you should use debug.print when using SQL, this would allow you to view the SQL and paste it into the query design window to see if it worked. The debug.print line can be commented out when everything is working. When you are unfamiliar with SQL, there is a lot to be said for using the query design window to build your queries. You can then cut the SQL from SQL View and add quotes etc.
EDIT re Question Part 2
Dim db As Database
Set db = CurrentDB
If IsNull(Me.TxtStockValue) Then
MsgBox "Please Select An Item To Update Stock " _
& "And Ensure A Value Has Been Entered"
Else
db.Execute SQLDelete1, dbFailOnError
''DoCmd.SetWarnings False
db.Execute SQLStory, dbFailOnError
db.Execute SQLDelete2, dbFailOnError
db.Execute SQLUpdate, dbFailOnError
''DoCmd.SetWarnings True
End If