473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to copy data from a Subform to a table using VBA

greeni91
I am currently in the process of remaking a database that was created a long time ago. I have hit a bit of a snag when it comes to using subforms.

I have a main form called "New Fixtures"

In this form I have one field called "Fixture"

I also have one subform called "ChoosenParts_subform"

In the subform I have 2 fields called "Part" and "Operation"

What I am trying to achieve is:

I want a form that you enter a specific fixture number. Then I will enter the Part Number(s) and Operation(s) for this fixture in the subform. When I am done I will click on a command button called "cmdSave" this will copy the data I have just entered into my main form and subform and place it as a new record in a table called "Part".

My code is as follows:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSave_Click()
  2.  
  3. Dim strSQL As String
  4. Dim Fixt As Variant, Part As Variant, Op As Variant
  5.  
  6.  
  7. Fixt = Me.FixtureNumber
  8. *Part = Forms![New Fixture]![ChoosenParts_subform].Form.["Part"]
  9. *Op = Forms![New Fixture]![ChoosenParts_subform].Form.["Operation"]
  10.  
  11.        With Me
  12.  
  13.            strSQL = "INSERT INTO [FixtPart] " & _
  14.                                 "([Fixture] " & _
  15.                                 ",[Part] " & _
  16.                                 ",[Operation] " & _
  17.                     "VALUES      ('%F' " & _
  18.                                 ",'%P' " & _
  19.                                 ",'%O')"
  20.  
  21.            strSQL = Replace(strSQL, "%F", Fixt)
  22.            strSQL = Replace(strSQL, "%P", Part)
  23.            strSQL = Replace(strSQL, "%O", Op)
  24.  
  25.  
  26.            Call CurrentDb.Execute(strSQL)
  27.            MsgBox "Fixture Information Updated!", vbOKOnly, "...Complete..."
  28.        End With
  29.  End Sub
Where I have placed an asterisk is where the Debugger is getting an error and returning a value "Empty".

Any help on this is greatly appreciated.

Regards,

greeni91
Jan 7 '13 #1

✓ answered by NeoPa

Forms![New Fixture] is not required as Me is always a reference to the associated form (and is less complicated).

Otherwise your references are pretty close. Seth has already pointed out the need to lose the double-quotes, but otherwise they should work. If you look through Referring to Items on a Sub-Form you'll see an alternative is to replace the ".Form." with "!".
Expand|Select|Wrap|Line Numbers
  1. Part = Me.ChoosenParts_subform!Part
  2. Op = Me.ChoosenParts_subform!Operation

4 4345
Seth Schrock
2,965 Expert 2GB
I believe that the error on your lines 8 & 9 are caused by the fact that you have double quotes " around the field names Part and Operation. Those need to be removed. I haven't looked at the rest of the code other than to wonder why the With/End With statements. I don't see where any Me.something is used. Not being used won't produce an error (I think, though I have never tried it), but seems unnecessary.
Jan 7 '13 #2
NeoPa
32,556 Expert Mod 16PB
Forms![New Fixture] is not required as Me is always a reference to the associated form (and is less complicated).

Otherwise your references are pretty close. Seth has already pointed out the need to lose the double-quotes, but otherwise they should work. If you look through Referring to Items on a Sub-Form you'll see an alternative is to replace the ".Form." with "!".
Expand|Select|Wrap|Line Numbers
  1. Part = Me.ChoosenParts_subform!Part
  2. Op = Me.ChoosenParts_subform!Operation
Jan 7 '13 #3
Thanks guys,

I have followed NeoPa's way of writing the code and it is working perfect now.

Here is a copy of the working code for reference:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSave_Click()
  2.  
  3. Dim strSQL As String
  4. Dim Fixt As Variant, Op As Variant
  5. Dim Prt As String
  6.  
  7.  
  8. Fixt = Me.FixtureNumber
  9. Prt = Me.ChoosenParts_subform!Part
  10. Op = Me.ChoosenParts_subform!Operation
  11.  
  12.        With Me
  13.  
  14.            strSQL = "INSERT INTO [FixtPart] " & _
  15.                                 "([Fixture] " & _
  16.                                 ",[Part] " & _
  17.                                 ",[Operation] )" & _
  18.                     "VALUES      ('%F' " & _
  19.                                 ",'%P' " & _
  20.                                 ",'%O')"
  21.  
  22.            strSQL = Replace(strSQL, "%F", Fixt)
  23.            strSQL = Replace(strSQL, "%P", Prt)
  24.            strSQL = Replace(strSQL, "%O", Op)
  25.  
  26.  
  27.            Call CurrentDb.Execute(strSQL)
  28.            MsgBox "Fixture Information Updated!", vbOKOnly, "...Complete..."
  29.        End With
  30.  End Sub
Jan 8 '13 #4
NeoPa
32,556 Expert Mod 16PB
I'm pleased we could help :-)
Jan 8 '13 #5

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

Similar topics

2
by: isa | last post by:
Hello everyone, i want to transfer/copy data from MSDE to SQL Server 2000 and from SQL Server to MSDE, through Stored Procedures , not using a "Replication", kindly tell me how i make a SP for...
1
by: romeodionisio | last post by:
How to copy data in table in a different SQL Server?.... Can anyone help me?.. Thanks....
15
by: BaneMajik | last post by:
I am working with an Access 2003 inventory database. When a piece of equipment goes bad we junk it and delete it from the database. We have been copying the record to an excel form for storage just...
8
by: blakerrr | last post by:
Hi All, Is it possible to create a carbon-copy of a table using VBA? I have a table called 'Junction' that stores the structure of a machines assemblies and subassemblies, and I need to create a...
1
by: rahullko05 | last post by:
Hi, i am building a small forum site as my final year project & stuck in a very trivial problem. I have a table which are varchar type of data & i am trying to fetch data from table based on...
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...
1
by: rajarameshchint | last post by:
import java.util.*; import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.awt.print.*; public class sd7 extends JFrame implements...
0
by: neelsfer | last post by:
I use the following code to copy data from one table (Fis_CaptureT) to another table (Fis_CaptDataT) in real time. It works 100%, except when you make a typo, because that also gets copied then to...
11
by: flametail | last post by:
I have a php script that shows all the data in a table. I want it to have check boxes next to each line of this data that it puts out, so I can check the boxes next to certain entries, and when I...
1
by: vikas shankdhar | last post by:
suppose when i have <select> <option value="mumbai">mumbai</option> <option value="banglore">banglore</option> <option value="delhi">delhi</option> <option...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.