Access VBA update a column to concatenate - sql

I was trying to update a column from a table in my Access database with following VBA code but it did not work as nothing happpened to that column. I would like to concatenate 5 columns into one. This update statement would work in SQL server but obviously not here.
currentdb.Execute "update sys_AAAA_AAAA set ABI=AAT+AAU+AAV+AAW+AAX"
Any advice? Thanks.

Perhaps some fields are Null?
Try using the string "concatenator" preferred by Access, ampersand:
CurrentDb.Execute "update sys_AAAA_AAAA set ABI = AAT & AAU & AAV & AAW & AAX"

Related

Run one MS Access SQL script on a particular Table chosen by user

I have a MS Access 2016 database (*.accdb) with 20+ Tables. Fields in each of them vary slightly from Table to Table. I've no VBA experience, so I'm sticking only to the SQL query below (redacted).
SQL script
myvar below is the parameter I'd like to be prompted when the script is run so that I enter the Table I want the changes applied to.
PARAMETERS
[myvar] TableID;
UPDATE
[myvar]
INNER JOIN
Excel_Data ON [myvar].[Part Number] = Excel_Data.[Part Number]
SET
[myvar].[Value] = '?',
[myvar].Description = Excel_Data.Description,
[myvar].[Ref] = '?'
.
.
.
WHERE
[myvar].Description Is Null;
Output
Error message:
Too few parameters. Expected 0.
What I need
I prefer a solution for above in a SQL script form as above, not involving VBA, preferably. I'd like to enter the Table name when prompted so the script knows which table to UPDATE. FYI: The PARAMETERS work when it is not a Table as I've shown in my script above.
Help/advise is highly appreciated.
EDIT 1
Since it seems not possible to use parameters as Table names, could you suggest a VBA solution? A sample code, perhaps?
As said in the comments, you can't really solve this without VBA.
You can store your SQL query in a string, and use a placeholder to indicate the tablename. Then get the tablename using an inputbox and replace the placeholder with the tablename.
Dim sqlString As String
sqlString = "UPDATE [%Placeholder%] " & vbCrLf & _
"INNER JOIN Excel_Data ON [%Placeholder%].[Part Number] = Excel_Data.[Part Number] " & vbCrLf & _
"SET [%Placeholder%].[Value] = '?', " & vbCrLf & _
...
"WHERE [%Placeholder%].Description Is Null;"
sqlString = Replace(sqlString, "%PlaceHolder%", InputBox("Enter a tablename"))
CurrentDb.Execute sqlString
In a more mature solution, I'd create a form with a combobox containing all available table names, and add a function to sanitize tablenames (replace "]" with "]]")

MS Access sql - Update query syntax

Seems a small issue with an sql update query. But I can not get my head around this issue. Not very much acquainted with sql.
Based on a selection, selected rows from a table (7DARphases) are copied and inserted to another table (7tblDAR). And the Project ID (PRJID column) for all the inserted rows should be updated with a fixed value (PID) taken from the form.
Issue is: I am facing syntax issue in the part where the fixed value needs to be inserted in the PRJID column of added rows.
Code is:
Set dbs = CurrentDb
PID = Me.PRJID.Value
sqlstr = "INSERT INTO 7tblDAR (Phase, Deliverable, Link_PIM_temp, Link_PIM_WB, Description, Accept_criteria, Deliv_type, TollgateID, Workstream, Accountable, ClientRACI) SELECT [7DARphases].Phase, [7DARphases].Deliverable, [7DARphases].Link_PIM_temp, [7DARphases].Link_PIM_WB, [7DARphases].Description, [7DARphases].Accept_criteria, [7DARphases].Deliv_type, [7DARphases].TollgateID, [7DARphases].Workstream, [7DARphases].Accountable, [7DARphases].ClientRACI FROM 7DARphases WHERE ((([7DARphases].SolType) Like '*2PP*')) ORDER BY [7DARphases].Phase, [7DARphases].Deliverable;"
sqlUpdt = "update 7tblDAR set PRJID = '" & Me.PRJID.Value & "' from 7tblDAR where tblDAR.PRJID = """""
dbs.Execute sqlstr, dbFailOnError
dbs.Execute sqlUpdt, dbFailOnError
The 'sqlstr' works fine and inserts rows.
But 'sqlUpdt' gives error:
"Run-time error 3075: Syntax error (missing operator) in query expression ..."
Can you please help me out with this.
Plus, if possible, can you suggest to perform this action in one query itself.
Why not put the value in when you insert the values?
sqlstr = "INSERT INTO 7tblDAR (Phase, Deliverable, Link_PIM_temp, Link_PIM_WB, Description, Accept_criteria, Deliv_type, TollgateID, Workstream, Accountable, ClientRACI, PRJID)
SELECT . . .,
'" & Me.PRJID.Value & "'
WHERE [7DARphases].SolType Like '*2PP*')
ORDER BY [7DARphases].Phase, [7DARphases].Deliverable;"
This saves the trouble of having to update it later.

MS ACCESS VBA: type mismatch error in recordset name into SQL statement string

How should I write the name of the recordset correctly? I have a type mismatch error on & rsg & at "sq3=..." line. Thanks in advance.
Dim rsG As DAO.Recordset
Dim sq2, sq3 As String
sq2 = "SELECT * from GeneralTable "
sq2 = sq2 & "where gsede='LION' and (gExclu is null) and (gAda is null) "
sq2 = sq2 & "order by gnomb,gnif;"
Set rsG = CurrentDb.OpenRecordset(sq2)
sq3 = "UPDATE " & rsG & " SET gsede ='AA' WHERE gsede='LION'"
DoCmd.RunSQL (sq3)
You are mixing up totally different ways to update data - UPDATE SQL and VBA recordset.
If you want to update a recordset row by row, you do something like
Do While Not rsG.EOF
If rsG!foo = "bar" Then
rsG.Edit
rsG!gsede = "AA"
rsG.Update
End If
rsG.MoveNext
Loop
Do the update using a single query:
update generaltable
set gsede = 'AA'
where gsede ='LION' and (gExclu is null) and (gAda is null);
If you do the update off of rsG, then you'll likely do a separate update for each row, and that is inefficient.
You can't mix and match SQL and recordset objects. You can either build a full update statement that includes the logic of the first select (as other answer suggests), or you can open a dynamic recordset and loop through the records programmatically to make the updates.
That being said, looping through large recordsets programmatically to make updates is usually better handled by a bulk SQL statement, unless it is essential to consider each row individually inside the code - which in your basic example would not be the case.
MS has a good article on the DAO.Recordset object. There is a dynamic-type Recordset example about halfway down.

ms-access: update record where some field = textbox value

can someone please help me with the following query
i need to update a datasheet (table) in access through a form:
i will have something like this
SQLtext = "update table1 set column1="sometext" where column2=textbox1.value"
DoCmd.RunSQL SQLtext
is this possible to do?
i have a textbox on a form and when i click a button on that form i want to update data in the datasheet where one of the columns is equal to the value property of a textbox
thank you!
This is what you need (note the subtle change)
SQLtext = "update table1 set column1='sometext' where column2='" & textbox1.value & "'"
DoCmd.RunSQL SQLtext
Note: For production code you will want to escape out any single quotes in the textbox1.value string using the string replace function, otherwise you will get a SQL error whenever a user types a single quote in that field.

"Null" value in Queries

I'm trying to run code that will copy fields into a new table, moving them from a _New table to the original table. The VBA code that does this works as such:
SQLStatement = CStr("INSERT INTO " & TName & " SELECT * FROM " & TName & "_New")
Log.WriteLine "Running query with string """ & SQLStatement & """ "
QueryTimer = Timer
DoCmd.RunSQL SQLStatement
Log.WriteLine "Query time: " & (Timer - QueryTimer)
The log is just a handful of procedures in a class module I threw together. Its output on the error is
#142921: Running query with string "INSERT INTO Records SELECT * FROM Records_New"
#142941: Error Date/Time: 7/21/2009 2:29:40 PM
#142941: Error # & Description: 3162, You tried to assign the Null value to a variable that is not a Variant data type.
I can confirm that TName and SQLStatement are both valid strings at the time the SQL operation is run, and that the sources (Records and Records_New) are both valid. Option Explicit is set elsewhere in the file to avoid any confusion from typos. The error is thrown on the DoCmd line.
Why would this have a Null value, even though DoCmd.RunSQL doesn't return a value?
Can you post the table descriptions for Records and Records_New tables?
I would wager that you are trying to insert a NULL value into one of the columns of the "Records" table (and the column description is NOT NULL).
Hope this helps.
I think it will help if you also change the insert statement to be more specific about which columns it is inserting/selecting. You are asking for bugs by being so non-specific.
This may seem like it is non-responsive to your answer, but I suspect that the columns in the select table and destination table are either not lined up, or there is a field in the destination table that disallows null.
Try this:
In a new Query (in SQL view) paste your query "INSERT INTO Records SELECT * FROM Records_New" in and try to run it manually. I bet you get a more specific error and can troubleshoot the query there before running it with the added complexity of the code around it.
INSERT INTO Statement (Microsoft Access SQL)
Your SQL INSERT statement is incorrect - it should be:
INSERT INTO Records SELECT * FROM [Records_New];
Here's what you need to use:
CStr("INSERT INTO " & TName & " SELECT * FROM [" & TName & "_New)"];")
Maybe Timer needs parens?
QueryTimer = Timer()