Access unbound form fields to update multiple tables - ms-access

My Database has 3 tables Inventory (Item Name, Barcode, Unit Type), In (Lot #, Barcode, Location, Expiration Date, Received Date), Out (Barcode, Team, Person Removing, Out Date). I have an unbound form for when I add a new Item to the main inventory. I would also like it to add the information to the In Table from the same unbound fields in the form. The unbound Field names are Item, Unit, Barcode, Lot, Expiration. I have tried using the “INSERT INTO” but cannot get it to work, I have never used it before.

Use this code: (make sure you keep In with [] since In is a reserved word)
strSQL = "INSETR INTO [In] (Item, Unit, Barcode, Lot, Expiration) "
strSQL = strSQL & "(" & Me.Item & "," & Me.Unit & "," & Me.Barcode & "," & Me.Lot & "," & Me.Expiration & ")"
DoCmd.RunSQL strSQL

Found It!!! Typed it all in by hand and it works not sure why but it does.
https://www.youtube.com/watch?v=Cgtk7LrtmJk

Related

How to select the recently added record in the listbox?

First off, my question is not in regards to selecting the last added record in a List Box. That can be achieved using this:
Me.MyList.Selected(Me.MyList.ListCount - 1) = True
Basically, I have a Form with a List Box that is bounded to a table. Let's call this table tblMyData. In this table, I have 4 fields, lets call it ID, FirstName, MiddleName, LastName.
I have 3 Text Box that corresponds to the appropriate FirstName, MiddleName, and LastName field values that can be entered.
When the form loads up, the List Box queries the table for data and displays it appropriately.
I've implemented the functionality to add the user input data to the tblMyData table:
Private Sub addRecord_Click()
CurrentDB.Execute "INSERT INTO tblMyData (FirstName, MiddleName, LastName)" _
& "VALUES ('" & Me.textBox_FirstName & "', '" & Me.textBox_MiddleName & "', '" & Me.textBox_LastName & "')"
Me.MyList.Requery
End Sub
The problem is that the tblMyData is sorted first by FirstName, then by LastName. Once the record has been added to the table and I call Me.MyList.Requery, MyList pulls the data from tblMyData, which is already sorted, including the newly added record.
It would be difficult to know where that added record is if there are 1000s of records to scroll through in the list.
Therefore, is there way to go about highlighting the row in the List Box containing that recently added record to ensure it has been added correctly?
I would like to call it after Me.MyList.Requery.
Thanks.
No, there isn't.
But why not set the value of the ListBox to that of the newly entered value?
That would display that entry right away. Something like:
Dim Criteria As String
Criteria = _
"FirstName = '" & Me.textBox_FirstName & "' And " & _
"MiddleName = '" & Me.textBox_MiddleName & "' And " & _
"LastName = '" & Me.textBox_LastName & "' And " & _
"YesNoField = " & Me.checkbox_Something & ""
Me!MyList.Value = DLookup("ID", "tblMyData", Criteria)

Look for duplicate records before adding a record

I clipped this from another post, since it is similar to what I need. Instead of using me.ID, etc, I need to reference a field in another table, specifically [Formdate] in a table named Brief Sheet.
I tried many variations, such as table "Brief Sheet".Formdate but no luck
What would be the proper syntax?
rst.FindFirst "[ID] <> " & Me.ID & _
" AND [TitleText] = '" & Me.TitleText & _
"' AND [UnitCode] = " & Me.UnitCode & _
" AND [AcademicYear] = " & Me.AcademicYear & _
" AND [Titleofchapterjournalarticle] = '" & Me.Titleofchapterjournalarticle & "'"
Use Dlookup. In the snippet you posted, the coder has already instantiated a recordset and is using recordset methods to find a record within it. What you said you want to do is actually a little simpler.
Below is a test procedure to illustrate getting your date value from your table.
You didn't clarify what criteria you would use to select the proper FormDate from the table "Brief Sheet", so I included "ID=3", which will select the FormDate record, whose ID=3. Adjust that as necessary.
Also, if your table name really is "Brief Sheet", and you have the ability to rename it, I highly recommend establishing some naming convention rules for your tables, first not having any spaces. Even Brief_Sheet would make your life easier down the road.
Public Sub Test1()
'dimension a variable to hold the date value
Dim datFormDate As Date
'fill the variable with the value you need to reference
datFormDate = DLookup("FormDate", "Brief Sheet", "ID = 3")
'Print the value to the "immediate" pane (just for testing)
Debug.Print datFormDate
'If you're running this code from within your form module, you can assign
'the value in your variable to a field in your table as such:
me.DateFieldtxtbx = datFormDate
End Sub

How to get an Access Userform to update an existing record

I'm trying to get an access userform button to run an update query.
The parameters I have are:
Invoice number (should update Current ICB.Invoice number)
Material Code (should update Current ICB.Material Code)
Amount USD (Should update current ICB.Amount USD)
Username (Should update current ICB.Owner)
Vendor Code (Should update current ICB.Vendor code)
Record Number (Should = current ICB.ID)
I want to use a button on the form to check for a record that has the same record number and update the fields listed above with the values entered into the userform. I would use an update query but writing SQL into VBA is not my strong point. Any ideas?
In the OnClick event of the button, do something like this:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from MyTable where RecordNumber = " & ICB.ID & "")
rec.Edit
rec("InvoiceNumber") = ICB.Invoice_number
rec("MaterialCode") = ICB.Material_Code
etc...
rec.update
rec.close
Use your actual table and field names, but that's the general idea.
If you absolutely insist on using a query, then you need to write the SQL in code in a similar way and use DoCmd.RunSQL. Something like this:
txtSQL = "UPDATE MyTable SET InvoiceNumber = " & ICB.Invoice_number & ", " & _
" MaterialCode = " & ICB.Material_Code & "" & _
etc...
" WHERE RecordNumber = " & ICB.ID & ""
DoCmd.RunSQL txtSQL
This assumes you're only using integers. If you're storing the data as text, it needs to be surrounded by single quotes.
" MaterialCode = '" & ICB.Material_Code & "'" & _

Insert statement in access gives error

I have been working on creating an extremely simple database with only 4 tables and only a few pieces of information per column. One of my tables is called "Customer" and inside of this table there or 4 columns for information.
I have a button on my "AddCustomerForm" that runs the following command
Private Sub cmdadd_Click()
CurrentDb.Execute "INSERT INTO Customer(Customer ID, Email, Identifier) " & _
= VALUES(Customer ID, Email, Identifier)
End Sub
My Add customer form looks like this:
Could someone please point out what I am messing up? The error I receive is :
Syntax error.
There's a few issues I see - is [Customer ID] an autonumber field? If so don't include it.
Also - if you're running a Manual Insert I assume your form is NOT bound to your table, though I begin to wonder why Customer ID is shown on the form as being editable?
Finally it looks like Location is a numeric ID belonging to ID field of the Location dropdown that fills in the Business ID field
This will help you debug your SQL and show us what's wrong
Add it to your button and show us the value shown in Immediate Window when the code halts
Dim strSQL as string
strSQL = "INSERT INTO Customer ([Customer ID], Email, Identifier) VALUES (" _
& me.[Customer ID] & ",""" & Me.[Email] & """,""" & Me.[Identifier] & """)"
Debug.print strSQL
CurrentDb.Execute strSQL
If Customer ID is AutoNumber try this instead (assuming form is UNBOUND) and Location is ID value of first column of dropdown
Dim strSQL as string
strSQL = "INSERT INTO Customer (Email, Identifier) VALUES (" _
& me.[Customer ID] & ",""" & Me.[Email] & """, & Me.[Identifier] & ")"
Debug.print strSQL
CurrentDb.Execute strSQL
Private Sub cmdadd_Click()
CurrentDb.Execute "INSERT INTO Customer ([Customer ID], Email, Identifier) VALUES([Forms]![MyFormName]![CustomerIDTextboxName], [Forms]![MyFormName]![EmailtextboxName], [Forms]![MyFormName]![IdentifierTextboxName]);"
End Sub
Access requires brackets around any field name with a space. I also deleted the = before VALUES and changed the values to reference your form controls, which you will have to name appropriately. You also need a semi-colon to complete the statement and need to close your double-quotes.
This page might help with syntax.

MS-Access 2003 - Expression in a Text Box on a form

just wondering when using an expression on a form in a text box, to return a value from a table, can the expression have multiple tables in the expression to return the value?
the tables are linked and I can return the value in a query, so I figured that Access would be able to do it with this method as well????
=DSum("[tblMain]![Revenue]","tblMain","[tblMain]![Quarter]=3 AND [tblMain]![Region]='NorthEast'" AND [tblOffice]![Location]='NewYork'")
this is the expression that I entered into my text box, without the reference to the 2nd table it works fine, but once I had it, I get the flickering error message in the text box (just as on a report)......
I know this method is probably used more in reports than forms, but I am novice, and trying to come up with a dashboard solution that returns lots of facts quickly per department. I am using this in the "Control Source" field of the data tab of the properties window, not VB. Mainly because I do not know how to get it to work with VB.
Thanks for the help as always!
As far as I know, you cannot refer to more than one table or query in a domain aggregate function. As grazird says, how are these tables related? Let us say it is on tblMain ID, you can build a query called, say, qryMainOffice, the SQL (SQL View, Query Design window) would look something like:
SELECT [tblMain].[Revenue],[tblMain]![Quarter],[tblMain]![Region],
[tblOffice]![Location]
FROM tblMain
INNER JOIN tblOffice
ON tblMain.ID = tblOffice.MainID
DSum would then be (remove line break):
=NZ(DSum("[Revenue]","qryMainOffice",
"[Quarter]=3 AND [Region]='NorthEast' AND [Location]='NewYork'"),"Not found")
You could also use a recordset or query in VBA to return the value.
EDIT re COMMENT
To use the above in VBA, you either need to add parameters or use a string:
''Reference: Microsoft DAO 3.x Object Library
Dim rs As DAO.Recordset
Dim db As Database
Dim strSQL as String
Set db= CurrentDB
strSQL = "SELECT Sum(t.[Revenue]) As TotalNY" _
& "FROM tblMain t " _
& "INNER JOIN tblOffice o " _
& "ON t.ID = o.MainID " _
& "WHERE t.[Quarter]=3 AND t.[Region]='NorthEast' " _
& "AND o.[Location]='NewYork' " _
'' I have use aliases for simplicity, t-tblMain, o-tblOffice
'' If you wish to reference a control, use the value, like so:
'' & " AND [Location]='" & Me.txtCity & "'"
'' Dates should be formated to year, month, day
'' For joins, see http://www.devshed.com/c/a/MySQL/Understanding-SQL-Joins/
Set rs = db.OpenRecordset strSQL
If Not rs.EOF Then
Me.txtAnswer = rs!TotNY
Else
Me.txtAnswer = "N/A"
End If
You can also use different queries to return several results that can be shown with a list box or a subform:
strSQL = "SELECT TOP 5 o.[Location]," _
& "Sum(t.[Revenue]) AS TotRevenue" _
& "FROM tblMain t " _
& "INNER JOIN tblOffice o " _
& "ON t.ID = o.MainID " _
& "WHERE t.[Quarter]=3 AND t.[Region]='NorthEast' " _
& "GROUP BY o.[Location]"
The above would return revenue for quarter 3 for all locations in NorthEast region. If you want the top values of each group, you are looking at a more complicated query, which I will leave for now.
How are these tables related? Can you describe the relationship and any primary/foreign keys?
Also, referencing the table name is not necessary in the first parameter of this function (since it is already taken care of in the second one).
For example, your code could be:
=DSum("Revenue","tblMain","Quarter=3 AND Region='NorthEast'" AND [tblOffice]![Location]='NewYork'")
Just trying to save you some keystrokes and increase its readability. :)