This question already has answers here:
MS Access Too Many fields during UPDATE
(2 answers)
Closed 5 years ago.
I am trying to update all rows of a table after importing data from excel to access with this code:
enter code here
DoCmd.RunSQL "UPDATE ([tbl1] INNER JOIN [2] ON " _
& "[tbl1].[Nr]= [tbl2].[Nr]) " _
& "INNER JOIN [tblExcelImport] " _
& "ON [tbl1].[Nr] = [tblExcelImport].[Nr] " _
& "SET " & strSQL & " " _
& "WHERE [tblExcelImport].[Type]= 'TYPE1';"
strSQL contains following string:
"[tbl1].[ColumnA]= [tblExcelImport]![ColumnA], [tbl1].[ColumnB]= [tblExcelImport]![ColumnB]" ... and much more columns.
So my question is how many columns can I put in to strSQL inside of a UPDATE query
in access VBA? I tried it with different amount of columns but I could not really conclude a strict logic in that.
next time I'll do more research in here I promise :D
Solved in the link:
MS Access Too Many fields during UPDATE
Related
I am trying to pull data from an unlinked table to a form with unbound fields. I already have a form where you enter the data and send it to the table but this is to be able to pull it back and update.
This is what I have
DoCmd.RunSQL "SELECT * " & _
" FROM [" & Address & "].[BusinessAccountOpening]" & _
" WHERE [ID]= " & Me![txtID] & ""
Forms!frm_ViewUpdateRecords!Sub_Details.Form.subaddthedata
But getting the error. An RunSQL action requires an argument consisting of an sql statement.
Please help
DoCmd.RunSQL and CurrentDb.Execute are for action queries (delete, update, append) while you present a select query.
So call DoCmd.OpenQuery ... or assign the records to a recordset.
I have an MSaccess database with 2 tables (process and data_type). Each process can contain 0-many types of data_type. Within access, I have a form that displays a process with a list-box, (see pic), that has all of the data_types. The user can then check the data_types that each process has.
We are in the process of migrating the data from access to MySQL. As part of that I've created a process2data table that links the process and data_type table. Unfortunately, I don't know how to create a form in Access (we are still, for now, using that as our display engine) that accomplishes the same thing with the online data.
You can download what I'm trying to do here:
(For simplicity, I've put copies of the online data tables that are in mySQL as offline tables in Access.)
The multi-valued fields works only on MS-Access databases, to emulate we need to add VBA code and a table to store the values.
Create intermediate table, in this case I have created [ProcessList_DataType], the [checked] field will be used to include the data type, see picture:
Create a form for ProcessList and a subform for [Datatypes] and [ProcessList_DataType] combined tables. Don't forget configure the link master fields.
Add code to check the referenced values are present on intermediate table:
Private Sub Form_Current()
On Error GoTo ErrExit
If Not IsNull(Me!ProcessID) Then
sql = "INSERT INTO ProcessList_DataType(ProcessList,DataType) " & _
"SELECT " & Me!ProcessID & ",datatype_id " & _
"FROM DataTypes WHERE datatype_id not in " & _
"(SELECT datatype_id from FilterQuery " & _
" WHERE ProcessList = " & Me!ProcessID & " )"
Set db = CurrentDb()
db.Execute sql
cnt = db.RecordsAffected
If cnt > 0 Then
NeedRefresh = True
End If
End If
ErrExit:
End Sub
Enjoy!
I have found similar answers to this question, even on this site, however, the syntax has not worked for my database and I'm not sure what needs to be done. This data base is used to house audits for staff performance and accuracy. I am now in the midst of creating the forms and getting them to flow properly for the user.
When conducting an audit, the user will need to enter six specific fields into the first form. Those forms are Audit, Month, Year, Username, Location, Reviewer, and Date. The user will need to complete multiple audits, however, these six fields will always be the same.
I would like to copy these fields in the first form and carry them into the second form so the user does not have to repeat the information. Here is my current code (set to run on the click of a command button on the bottom of screen 1):
Dim strSQL As String
strSQL = "INSERT INTO [tblTripMem] (Audit, Month, Year, Username, Location, Reviewer, Date)"
strSQL = strSQL & " Values (" & Me.cboFP1Audit & "," & Me.Month & "," & Me.Year & "," & Me.Username & "," & Me.Location & "," & Me.Reviewer & "," & Me.Date & ") FROM [FPScreen1]"
strSQL = strSQL & "WHERE (currentrecord = " & Me.CurrentRecord & ")"
DoCmd.RunSQL (strSQL)
Each time I run this I receive the following error: "Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
I am new to Access and am unsure of what this means or how to fix it. All I know is that I'm not finding a solution. Can anyone help? I'd greatly appreciate it.
Here's a mock-up Access file illustrating a way to do what you're doing without using SQL:
With Form 1 open...
...complete the various fields:
Click the Copy to Form 2 button and this will open Form 2 and populate its fields with the data from Form 1:
Here's the VBA code on the Copy to Form 2 button's OnClick event:
Private Sub cmdCopyToFrm2_Click()
Dim frm As Form
DoCmd.OpenForm "Form2"
Set frm = Forms!Form2
With frm
!AuditRef = Me.cboFP1Audit
!AuditMonth = Me.txtAuditMonth
!AuditYear = Me.txtAuditYear
!AuditUsername = Me.txtAuditUsername
!Location = Me.txtLocation
!Reviewer = Me.txtReviewer
!AuditDate = Me.txtAuditDate
End With
End Sub
Note that when Form 2 opens, the textbox that the cursor defaults to might not seem to show any data; if you move away from that textbox it should magically show (don't know why it does this, but there you go).
INSERT INTO table (...) VALUES (...) cannot have a FROM or WHERE clause. You insert a single record with fixed values, not data from another table.
But once you delete these clauses, you will have other errors, because you need to format your string and date values correctly to get the INSERT query to work.
And then you will still be prone to SQL injection errors. It is safer to use Recordset.AddNew and .Update to add records.
See e.g. here: https://stackoverflow.com/a/34969410/3820271
(but without the loop)
I think my first submission might have been a bit confusing so I decided to rewrite it to better explain what I am attempting to do.
I have an Access database. In it there are two combo boxes, one containing all possible forms of annuity (that I'm dealing with) and the other combo box is the ones the user populates that apply to that particular client. This master list is fed from a table (tblPickAnnuityForms). This table has 3 fields, Name, VarName (these values match to another table), and Deleted (is a 1 or a 0 depending on if the user selected it). I also have a second table called tblPlanSpecs. This table, among other fields, has the fields that match up to the VarName field in the first table.
What I need to do, since tblPickAnnuityForms is basically a temporary table and it changes as you move between records (clients), is to repopulate it when you go to that record based on the values in tblPlanSpecs. Once tblPickAnnuityForms is populated based on the previous elections for this record (client), the two combo boxes are re-queried to display the proper values (ie the left box shows the remaining, unused annuity forms and the right box shows the forms that apply to this client.
Below is my attempt at doing this.
Dim db As DAO.Database
Dim rsList As DAO.Recordset
Dim rsData As DAO.Recordset
Dim CurrForm As String
Dim FormVal As Integer
Dim Plan As String
Set db = CurrentDb
Set rsList = db.OpenRecordset("tblPickAnnuityForms", dbOpenSnapshot)
Plan = [Forms]![FrmHome]![PlanNameCalc].Value
Set rsData = db.OpenRecordset("SELECT tblPlanSpecs.LifeAnnuity, tblPlanSpecs.FiveCC, tblPlanSpecs.TenCC, " _
& "tblPlanSpecs.FifteenCC, tblPlanSpecs.TwentyCC, tblPlanSpecs.FiveCertain, tblPlanSpecs.TenCertain, " _
& "tblPlanSpecs.FifteenCertain, tblPlanSpecs.TwentyCertain, tblPlanSpecs.FiftyJS, tblPlanSpecs.SixtySixJS, " _
& "tblPlanSpecs.SeventyFiveJS, tblPlanSpecs.HundredJS, tblPlanSpecs.MCR FROM tblPlanSpecs " _
& "WHERE tblPlanSpecs.PlanName='" & Plan & "'")
Do
CurrForm = rsList.Fields("VarName")
FormVal = rsData.Fields(CurrForm)
DoCmd.RunSQL ("UPDATE tblPickAnnuityForms " _
& "SET tblPickAnnuityForms.Deleted=" & rsData! & FormVal & " " _
& "WHERE (((tblPickAnnuityForms.VarName)='" & CurrForm & "'))")
MsgBox (CurrForm & "changed to " & FormVal)
rsList.MoveNext
Loop Until rsList.EOF
If Not rsList Is Nothing Then
rsList.CLOSE
Set rsList = Nothing
End If
If there is a better solution, perhaps I can go in a different direction. Currently this is bombing out in the loop where it says rsData! & FormVal It does not like using a variable to call a field. Ideally I would like to avoid specifically calling every variable by name in code when populating rsData. In other words, I want this to work no matter how many other options I add to my master list for the combo box, without going back in to add more items to select query.
Please let me know if I am unclear in my intended direction or methods. I could really use the help figuring out what is wrong.
Well, it seems I solved my own problem. I had so many ideas running through my head, I half-implemented one and forgot. The variable FormVal was already pulling the value I needed from rsData. I then tried to pull the value again using FormVal as the field variable. Anyway, below was the simple solution and everything works now.
DoCmd.RunSQL ("UPDATE tblPickAnnuityForms " _
& "SET tblPickAnnuityForms.Deleted=" & FormVal & " " _
& "WHERE (((tblPickAnnuityForms.VarName)='" & CurrForm & "'))")
In other words, I didn't need to do rsData![ & FormVal & ] (which I am sure is improper syntax) I just needed to use FormVal by itself.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working on an application which I intend on protecting with Usernames & Passwords stored on a MS SQL Server. I've been trying for a number of days to get Visual Basics to compare the text entered in the two text boxes with the Username and Password on the SQL Database but I can't figure out a way to do it. The connection to the databse is already established and I'm able to manipulate the data on the Server from my Application, but I just can't figure out the code for 'comparing' the credentials.
The database that I'm connecting to is called Members_Details and the Table is called Logins. The logins table only holds two columns Name & Passkey (Username & Password.)
What I've tried so far is this:
SQL = "Select * from logins " & _
"where Name like '%" & Me.Usernametxt.Text.Trim & "%' AND Passkey LIKE Me.Passwordtxt.Text.Trim & "%'"
But I don't know where to go after that :/
Any help would be appreciated, and please not security is not an issue at the moment.
The problem is caused by the embedding of the Passwordtxt textbox value inside the sql text.
SQL = "Select * from logins " & _
"where Name like '%" & Me.Usernametxt.Text.Trim & _
"%' AND Passkey LIKE '" & Me.Passwordtxt.Text.Trim & "'"
However, also if you don't want advice on security, do a little step forward and write
SQL = "Select * from logins " & _
"where Name like #uname " & _
"AND Passkey = #pass"
Dim sqlCommand = new SqlCommand(SQL, con)
sqlCommand.Parameters.AddWithValue("#uname", "%" & Me.Usernametxt.Text.Trim & "%")
sqlCommand.Parameters.AddWithValue("#pass", Me.Passwordtxt.Text.Trim )
Using reader = cmd.ExecuteReader()
if reader.HasRows Then
' user authenticated
Also, usually, the passwords are required to be typed exactly (case sensitive) and thus not retrieved using a LIKE operator and wildcards.
The wildcard is also a bit strange for the username while it is acceptable to use the LIKE operator to allow the username to be case insensitive