In my MSAccess database, I have this textbox "ProductName" where a user enters a value and presses a button to insert the value at the bottom of the table "Product". The column I want to add it to is "Product_ID" and my sql statement is
CurrentDb.Execute "INSERT INTO Tables!Product (Product_ID) VALUES ('" & Me.ProductName.Value & "')"
I found that this is breaking my code and I have been trying to fix it but cant seem to figure it out
I have also tried this
DoCmd.RunSQL "INSERT INTO Tables!Product (Product_ID) VALUES ('" & Me.ProductName.Value & "')"
Related
I have a form that insert data into a table , the problem is I can't insert value from my combo box into a table field which is ( lookup field) this field should store an integer (ID) that I lookup from a different table.
the error I have is
here is my code :
DoCmd.RunSQL ("INSERT INTO tblStocks([Stock Code], [Stock Name], [Stock Keeper1]) VALUES('" & txtCode & "','" & txtName & "'," & cmbStockKeeper1.Value & ")")
The problem simply was caused by my table design as I made the field (Stock Keeper1) indexed with no duplicate and I forgot about it, so the message was due to me trying to insert a value was already given to another record
I have written a simple code to loop through rows and insert values in column A (that is column containing full name) into an SQL Table. Something like this:
For i = 1 to LastRow
Command.CommandText = "INSERT INTO [TABLE] [Col1] VALUES ('" & Sheets("Sheet1").Cells(i, 1).Value & "')"
Next i
Issue arises when we have names like [O'Connell], which obviously creates a Bobby Table issue.
Are there any clever workarounds to avoid this?
Thanks
N.B. The full name is inserted from another SQL Table. In other words, perhaps a little difficult to edit.
You could use Replace to remove the problem character:
Command.CommandText = "INSERT INTO [TABLE] [Col1] VALUES ('" & _
Replace(Sheets("Sheet1").Cells(i, 1).Value,"'","") & "')"
I have a user form that inserts data into "user" table and "organizer" table when I click a button, user data gets inserted with no problem, but it prompts me to enter the value (organization_name) again in a small dialogue box -which supposed to take from the text field and insert into organizer table- ,
then gives me an error saying "ms access set one row null due to validation rule violation"
NOTE: I didn't put any validation rule for the "organization_name" anywhere
Private Sub InsertSqlIntoOrgTable()
Dim orgName As String, SqlOrgNameInsert As String
orgName = txtOrgName.Value 'takes the value from text field
SqlOrgNameInsert = "INSERT INTO ORGANIZER (ORG_NAME) VALUES (" & orgName & ") "
DoCmd.RunSQL SqlOrgNameInsert
End Sub
SqlOrgNameInsert = "INSERT INTO ORGANIZER (ORG_NAME) VALUES ('" & orgName & "') "
if the field name in table ORGANIZER is really ORG_NAME. Show us your complete table definition in case that this doesn't solve your problem. Because in your last question you posted:
sqlOrgInsertUsrId = "INSERT INTO ORGANIZER (USER_ID) VALUES (" & orgUserId & ")"
Both inserts run into the same table but try to create new independent rows. If USER_ID is primary key then your insert into ORG_NAME can't work that way.
You should learn more about SQL.
I am doing my best to build my first database, but I have come up against a problem I just cannot find an answer to. I am a complete newbie in this forum and writing any sort of code so please be gentle.
I am trying to create a new record in a table when a student's name is double clicked inside a list box which is inside a form.
List box where I want to take first (StudentID) column value from = lstStudent
Combo box where I want to take the second (CourseID) column value from: cboCourseID
Text box where I want to take third (NoteID) column value from = txtCourseNoteID
The new record is being created in the desired table and there are no incorrect code errors but there are no values being carried across to the fields. The autonumber is being created (AttendanceID) but the other columns are blank. Here is my code:
Private Sub lstStudent_DblClick(Cancel As Integer)
CurrentDb.Execute "INSERT INTO tblAttendance (StudentID, CourseID, NoteID) VALUES ('me.lstStudent','me.cboCourseID','me.txtCourseNoteID')"
End Sub
The fields are populated, so this isn't the issue. The formatting is correct for the target fields and I can't think of anything else in my way.
The new record is being created in the desired table and there are no
incorrect code errors but there are no values being carried across to
the fields. The autonumber is being created (AttendanceID) but the
other columns are blank.
With this INSERT statement, you're supplying text values for those 3 fields in the new row ...
INSERT INTO tblAttendance (StudentID, CourseID, NoteID)
VALUES ('me.lstStudent','me.cboCourseID','me.txtCourseNoteID')
However StudentID, CourseID, and NoteID are numeric fields, so will not accept those text values. In that situation, there is nothing the db engine can insert. You still get a new row added (with the new autonumber value in AttendanceID), but those other fields are empty.
If you include the dbFailOnError option with .Execute, Access will notify you about the problem (error #3464, "Data type mismatch in criteria expression") and abort the insert --- a new row will not be added ...
CurrentDb.Execute "INSERT INTO tblAttendance (StudentID, CourseID, NoteID) VALUES ('me.lstStudent','me.cboCourseID','me.txtCourseNoteID')", dbFailOnError
Use an approach similar to what #HarveyFrench suggested, but eliminate the single quotes from the VALUES list ...
Dim strInsert As String
strInsert = "INSERT INTO tblAttendance (StudentID, CourseID, NoteID)" & vbCrLf & _
"VALUES (" & Me.lstStudent.Value & "," & Me.cboCourseID.Value & "," & _
Me.txtCourseNoteID.Value & ");"
Debug.Print strInsert ' <- view this in Immediate window; Ctl+g will take you there
CurrentDb.Execute strInsert, dbFailOnError
Based on what you've told us, I suspect that will work, but I'm not sure it's the best way to reach your goal. You could open a DAO.Recordset and add the new row there. Or if you can use a bound form, Access would automatically take care of storing a new row ... without the need to write code.
Change this
CurrentDb.Execute "INSERT INTO tblAttendance (StudentID, CourseID, NoteID) VALUES ('me.lstStudent','me.cboCourseID','me.txtCourseNoteID')"
to be this
CurrentDb.Execute "INSERT INTO tblAttendance (StudentID, CourseID, NoteID) _
VALUES ('" & me.lstStudent & "','" & me.cboCourseID & "','" & me.txtCourseNoteID "');"
See also here for useful info
I have an Access application in which I need to do a 'mass-update'.
I have a form on which I have a dropdown, a listbox, and a checkbox. I also have a Update button, and when the user clicks it, I want to insert rows into my table with the same value for the dropdown and checkbox fields in all the rows, but I want different values for each row depending on the list items that were selected.
I know that multiple rows can be inserted into a table using the UNION SELECT statement, but how am I supposed to apply that with a listbox?
Can anyone please help?
Thanks in Advance
You can create an update statement for each item selected. For example:
strSQLBase="INSERT INTO Table (Field1, Field2, Field3) Values (" & Me.Dropdown _
& "," & Me.CheckBox & ","
For Each itm In Me.ComboBox.ItemsSelected
strSQL = strSQLBase & Me.ComboBox.Column(0, itm) & ")"
CurrentDb.Execute strSQL
Next