473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Writing from form/sub-form to a table

10 New Member
I have a form (Purchase Orders) and a sub-form in it (Purchase Order Details). The Main form contains Number of the PO (text box), Supplier (combo-box), Employee (combo-box), Status (combo-box) which contains 2 records (New and Done) and a date box (when the PO was created). The Sub-form (datasheet) contains Product (combo-box), Quantity and a Price field.

What I want to do is to add a button on the main form which will do next.

When the button is pressed a VBA code should be executed and do next. Take data from the Main form (Number of the PO, Status and the date) and the Sub-form (Product, Quantity and Price) and put all that into a table (StockMovements).

I managed to do that with the next code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOrder_Click()
  2. Dim strSQL As String
  3.  
  4.     strSQL = "INSERT INTO StockMovement (ID_Product, Status, Quantity, ID_PurchaseOrder) VALUES (" & Me.frmPurchaseOrderDetails_Subform.Form!comboboxProduct & ", '" & Me!txtStatus & "', " & Me.frmPurchaseOrderDetails_Subform.Form!txtQuantity & ", " & Me!txtID_PurchaseOrder & ");"
  5.     DoCmd.RunSQL strSQL
  6.     Me.Requery
  7.  
  8. End Sub
However, there are 2 problems:

As you can see I didnt add the date field because I get an error, cant remember which one it was exactly but I think 2075;
The code works without the date, but only adds one Product to the table, the first one. And in a Purchase Order there are usually more than one products.
Because Im totally new in VBA, I would kindly ask you to treat me like a newbie and explain more detailed, if possible.

I also tried to add a check box in the subform for each product so when I check it to do an afterUpdate and write the data to the table but culdnt figure out how to select the main form items in the VBA code. Anyhow, the first solution would be best because this one would require to check every product in the subform.

If more details needed please let me know.
Thanks!
Dec 12 '16 #1
19 1821
jforbes
1,107 Recognized Expert Top Contributor
Are your Purchase Orders and Purchase Order Details Forms Bound to a Table or Query or are they Unbound?
Dec 13 '16 #2
XYZ0
10 New Member
Yes they are, to tables.
Dec 13 '16 #3
jforbes
1,107 Recognized Expert Top Contributor
You'll probably want to try something like this instead. The code was created freehand and will not work without you tweaking it and getting it right for your environment:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOrder_Click()
  2.  
  3.     Dim strSQL As String
  4.  
  5.     strSQL = ""
  6.     strSQL = strSQL & "INSERT INTO StockMovement ( "
  7.     strSQL = strSQL & "  ID_Product "
  8.     strSQL = strSQL & ", Status "
  9.     strSQL = strSQL & ", Quantity "
  10.     strSQL = strSQL & ", ID_PurchaseOrder "
  11.     strSQL = strSQL & ") SELECT "
  12.     strSQL = strSQL & "  PurchaseOrderDetails.ID_Product "
  13.     strSQL = strSQL & ", PurchaseOrder.Status "
  14.     strSQL = strSQL & ", PurchaseOrderDetails.Quantity "
  15.     strSQL = strSQL & ", PurchaseOrder.ID_PurchaseOrder "
  16.     strSQL = strSQL & "FROM PurchaseOrder "
  17.     strSQL = strSQL & "LEFT JOIN PurchaseOrderDetails "
  18.     strSQL = strSQL & "ON PurchaseOrder.ID_PurchaseOrder= PurchaseOrderDetails.ID_PurchaseOrder "
  19.     strSQL = strSQL & "WHERE ID_PurchaseOrder=" & Me!ID_PurchaseOrder
  20.  
  21.     CurrentDB.Execute strSQL, dbFailOnError + dbSeeChanges
  22.     Me.Requery
  23.  
  24. End Sub
The version you have will only insert one record no matter what. The supplied version will insert a record for each Detail Record. If you need to debug this, put a break on the .Execute line and enter "?strSQL" into the Immediate Window and hit enter. It will display the SQL to you so that you can copy and paste it into a new Query to debug. ...How to Debug SQL String
Dec 13 '16 #4
XYZ0
10 New Member
Thanks a lot for your time. I tried the above code but I get an error "The specified field ID_PurchaseOrder could refer to more than one table listed in the FROM clause of your SQL statement (Run-time error 3079). I assume PurchaseOrder and PurchasOrderDetails refer to the tables and not the form? (sry for the noob question).
Back to the error, I have only one table named PurchaseOrder and the ID_PurchaseOrder appears only in the PurchaseOrdersDetails table as a foreign key and to the StockMovements table, also as foreig key.

Edit: I have included next :
Expand|Select|Wrap|Line Numbers
  1. strSQL = strSQL & "FROM PurchaseOrder, [PurchaseOrderDetails] "
But now I get a syntax error in JOIN operation
Dec 13 '16 #5
jforbes
1,107 Recognized Expert Top Contributor
I really didn't expect the code to work as it is. It's an example of how you can structure a SQL Insert Statement to do what you are attempting.

Yes, PurchaseOrder and PurchasOrderDetails refer to the tables. When using SQL, it's much easier to stick to SQL as much as possible. In fact in the example, the only reference to the Form is the line:
Expand|Select|Wrap|Line Numbers
  1. strSQL = strSQL & "WHERE ID_PurchaseOrder=" & Me!ID_PurchaseOrder
where it gets the current ID from the Form and uses it to determine what records to insert. Which should probably be:
Expand|Select|Wrap|Line Numbers
  1. strSQL = strSQL & "WHERE PurchaseOrder.ID_PurchaseOrder=" & Me!ID_PurchaseOrder
  2. OR
  3. strSQL = strSQL & "WHERE PurchaseOrder.ID=" & Me!ID_PurchaseOrder

I put a lot of assumptions into the example code, like Table Names, Field Names, and table relationships. If the field ID_PurchaseOrder only appears in the PurchaseOrdersDetails, then what is the corresponding field in PurchaseOrders? The JOIN will need to be updated to reflect this, as I assumed they were the same in both Tables.
Dec 13 '16 #6
XYZ0
10 New Member
Ok Im new in SQL, I read the code like this. Hey code in the table StockMovements in her fields ID_Product, Status, Quantity and ID_PurchaseOrder insert PurchaseOrderDetails.ID_Product, PurchaseOrder.Status, PurchaseOrderDetails.Quantity, PurchaseOrder.ID_PurchaseOrder FROM the tables PurchaseOrder and PurchaseOrderDetails and use a LEFT JOIN which will return all rows from the left table (PurchaseOrder), with the matching rows in the right table (PurchaseOrderDetails) and to do it some compere the ID_PurchaseOrder from the first table with the ID_PurchaseOrder from the second table WHERE the ID_PurchaseOrder is same as the one in the main form (didnt quite understand the where statement yet).
Now, I don understand the error I get, the code looks fine :(
Dec 13 '16 #7
jforbes
1,107 Recognized Expert Top Contributor
There are two types of SQL Insert Statements that are used the most often.
The first is what you were using: SQL INSERT INTO Statement If you read the article, you should notice that it only inserts one record into the Target Table.
The second is what I recommend you try to use: SQL INSERT INTO SELECT Statement Hopefully, you'll notice that the records that are INSERTED are from the SELECT statement, which is not limited to one record.

The key to making a SQL INSERT INTO SELECT Statement work is to have a well written SELECT statement. You can develop the SELECT Statement by itself in the Query Editor without the INSERT statement. You can then add the INSERT portion in the Query Editor or you can copy-and-paste in into VBA and get it working in Code. Regardless, getting the SELECT statement correct tends to be the biggest obstacle with this statement.
Dec 14 '16 #8
XYZ0
10 New Member
I did it. Opened the query builder, added the 2 tables, PurchaseOrders and PurchaseOrdersDetails and the selected the needed fields. The SQL text was like you wrote it, just instead the LEFT JOIN it was an INNER JOIN, however, I still get the same error "Syntax error in FROM clause 3131" when I added the [PurchaseOrdersDetails] in the FROM line then I get the syntax error in JOIN operation (3135). I read the link you posted but it states the situation when u take data from one table and copy to another but not when u take data from 2 or more tables and then copy to another one. I researched a bit fro 2 tables or more into another and found to use the UNION, but not quite sure how to do it in VBA.
Dec 14 '16 #9
jforbes
1,107 Recognized Expert Top Contributor
A UNION will merge two Sets of data. Usually the two sets of data are similar in structure, and typically have the same columns. I'm pretty sure this isn't what you are after.

A SQL JOIN will link two tables together on a Key column(s) so that they can be worked with as one Set.

On the surface the two may sound similar, but they aren't.

If you want to post your SQL, we can look at it see if it can be helped.
Dec 14 '16 #10
XYZ0
10 New Member
I thought so :D
I did read all the JOINs in w3school and the union.
The VBA/SQL is exactly the same as you wrote in the first post, just different names because I use another language, and not english unfortunately.
Would you like to take a look into the database? Just the language is different and probably it will confuse you :/
Dec 14 '16 #11
jforbes
1,107 Recognized Expert Top Contributor
Whatever you want to do. I'm not concerned about a different language as there should be enough keywords to figure out what is going on.
Dec 14 '16 #12
XYZ0
10 New Member
https://1drv.ms/u/s!AgExhJ1MpmCGiHJG8Fh2pBs_in_g
There you go. Here some help...
frmNarudzbenice = frmPurchaseOrders
frmNarudznebiceDetalji = frmPurchaseOrdersDetails
Proizvod = Product
Kolicina = Quantity
In the form frmNarudzbenice the button "Naruci" triggers the code on click..
Ask if you need something else to translate :D
And big thanks!
Dec 14 '16 #13
jforbes
1,107 Recognized Expert Top Contributor
If you don't mind, would you Zip the file and attach it here?

If you use the Advanced Editor, there is an Additional Options section that allows you to Attach Documents.
Dec 14 '16 #14
XYZ0
10 New Member
For some reason it fails to upload, and the zip has only 2MBs and max is 5MBs. Where else could I upload it? is mediafire or dropbox fine?
Dec 14 '16 #15
jforbes
1,107 Recognized Expert Top Contributor
Maybe a public DropBox link?
Dec 14 '16 #16
jforbes
1,107 Recognized Expert Top Contributor
Here you go:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdNaruci_Click()
  2.  
  3.     Dim strSQL As String
  4.  
  5.     strSQL = ""
  6.     strSQL = strSQL & "INSERT INTO KretanjeRobe ( "
  7.     strSQL = strSQL & "  ID_Proizvoda "
  8.     strSQL = strSQL & ", VrstaKretanja "
  9.     strSQL = strSQL & ", Kolicina "
  10.     strSQL = strSQL & ", ID_Narudzbenice "
  11.     strSQL = strSQL & ") SELECT "
  12.     strSQL = strSQL & "  NarudzbenicaDetalji.ID_Proizvoda "
  13.     strSQL = strSQL & ", Narudzbenica.Vrsta "
  14.     strSQL = strSQL & ", NarudzbenicaDetalji.Kolicina "
  15.     strSQL = strSQL & ", Narudzbenica.ID_Narudzbenice "
  16.     strSQL = strSQL & "FROM Narudzbenica "
  17.     strSQL = strSQL & "INNER JOIN NarudzbenicaDetalji "
  18.     strSQL = strSQL & "ON Narudzbenica.ID_Narudzbenice = NarudzbenicaDetalji.ID_Narudzbenice "
  19.     strSQL = strSQL & "WHERE Narudzbenica.ID_Narudzbenice = " & Me!ID_Narudzbenice
  20.  
  21.     CurrentDb.Execute strSQL, dbFailOnError + dbSeeChanges
  22.     Me.Requery
  23.  
  24. End Sub
There were two typos, I'll let you figure them out. =)

I included this image of the Debugging so that it may help you in the future:



By putting a break in the Code, the SQL String can be printed out in the Immediate Window, then the SQL can be Copied and Pasted into a Query Editor so that the SQL can be tweaked without running the code a bunch of times.
Attached Images
File Type: jpg Narubzbenica.jpg (65.8 KB, 482 views)
Dec 14 '16 #18
XYZ0
10 New Member
Expand|Select|Wrap|Line Numbers
  1. strSQL = strSQL & "WHERE Narudzbenica.ID_Narudzbenice = " & Me!ID_Narudzbenice
I figured out only this typo, cant find the other one. Will need to see how to enable the immediate window.
Dam* so much time wasted for ta typo -_- lol
Thanks a lot, u rock :D
Btw. im curious what was the second typo xD

One more quick thing. Can I add some code to gray out the button after its pressed, but guess I can google that
Dec 14 '16 #19
jforbes
1,107 Recognized Expert Top Contributor
Here is the other one:
Expand|Select|Wrap|Line Numbers
  1. strSQL = strSQL & "FROM Narudzbenica, [NarudzbenicaDetalji]"
Glad your up and running. If you get stuck on the Enabling/Disabling of the Button, start another question and include what you've done and we'll help.
Dec 15 '16 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Josh C. | last post by:
Hello everyone. I'm a bit of an Access newbie, so please bear with me. Please go to http://www.dumoti.com/access/ to view the database - 536kb. I'll go straight into examples: In the form...
4
by: Don Seckler | last post by:
I am building an application to track the distribution and returns of copies of magazines. Copies of a magazine that are unsold are returned by the retailer to the wholesaler. They are...
3
by: Simone | last post by:
Hi All, I have a Find Record button setup in most of my forms in order to find a specific customer's details. I have just noticed today though that this search will only find the customer if it...
9
by: PC Datasheet | last post by:
I'm stuck on something that seems should be easy and I need some help. My main form has an option group with five options. My subform chooses from different lists depending on which option is...
10
by: Thelma Lubkin | last post by:
My form/subform combination that allows user to display 'ColorSet' records, move to other records via a selector, to add,delete, and edit them, and to manage the related 'Color' records from the...
1
by: grammy | last post by:
I have a Form with one Subform. After input, I look at the tables, and not all of the info is coming thru.I have looked at the child,master link properties, and they look okay. The main form is a...
2
by: bcarson86 | last post by:
Hi, I am trying to append a list of part numbers which is generated in a seperate table into a subform table which is on a main form. My main problem with getting it to work is that I need the part...
1
by: troy_lee | last post by:
I have a table (Table A). It has one field, a PK. It is in a 1:M with another table (Table B). I am having trouble with a form/subform setup to view the 1:M records. On the parent form, there is...
4
by: Ploepsie | last post by:
Hey, first of all I want to state that I am very new to using Access and therefor the question might be relevantly easy to solve but I have spent hours thus far to find the answer online and in...
10
by: mdewell | last post by:
I am using Access 2007. I have a form with fields that I want to copy to a subform. I nave an ADD button that can sopy the data to the subform, so the subform is acting as a clipboard. I can get...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.