473,405 Members | 2,344 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,405 software developers and data experts.

Append to a table looping VBA code - need to add more data

I added the 2 str's below. How do I add them into the SELECT INTO while still keeping the looping syntax? The field names in the table (tShipDetails) are Qty and StockID.
Any help is appreciated.
KM
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdShip_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. Dim dteStartDate As Date
  3. Dim intFreq As Integer
  4. Dim strQty As String
  5. Dim strStockID As String
  6. Dim i As Integer
  7.  
  8. dteStartDate = Me.txtShipStart
  9. intFreq = Me.txtShipDur
  10. strTons = Me.txtQty
  11. strItemID = Me.cboStockID
  12. i = 0
  13.  
  14. Do Until i = intFreq
  15. CurrentDb.Execute "INSERT INTO tShipDetails ( [StartDate]) SELECT #" & dteStartDate & "# AS FieldDate ;", dbFailOnError
  16. dteStartDate = DateAdd("m", 1, dteStartDate)
  17. i = i + 1
  18.  
  19. Loop
  20. End Sub
Jun 10 '09 #1
11 3207
ADezii
8,834 Expert 8TB
@KashMarsh
  1. Why are you Declaring Quantity as a String?
  2. Your Variable Declarations and Assignments seen to be out of sync.
  3. Why is this Code being executed in the MouseUp() Event?
  4. Stating the above, a General Template would be (Syntax not Verified):
    Expand|Select|Wrap|Line Numbers
    1. Dim dteStartDate As Date
    2. Dim intFreq As Integer
    3. Dim lngQty As Long
    4. Dim strItemID As String
    5. Dim i As Integer
    6.  
    7. dteStartDate = Me.txtShipStart
    8.  
    9. intFreq = Me.txtShipDur
    10. lngQty = Me.txtQty
    11. strItemID = Me.cboStockID
    12.  
    13. i = 0
    14.  
    15. Do Until i = intFreq
    16.   CurrentDb.Execute "INSERT INTO tShipDetails ([StartDate], [Qty], [StockID]) VALUES (#" & _
    17.   dteStartDate & "#", lngQty & ", '" & strItemID & "')"
    18.     dteStartDate = DateAdd("m", 1, dteStartDate)
    19. i = i + 1
    20. Loop
Jun 10 '09 #2
ADezii thank you for your reply and expertise. I really appreciate you.

To answer your questions:
1. lng should be used instead of str, I just didn't know.
2. Yes, I noticed a typo on strTons - should be strQty, but really should be lngQty.
3. I prefer to use mouseUp instead of onClick, is there a better option?

Again, thanks you have helped me tremendously.
KM
Jun 10 '09 #3
ADezii,
I'm getting a Run-Time error '3421'
Data Type conversion error.

It's highlighting this part:

CurrentDb.Execute "INSERT INTO tShipDetails ([StartDate], [Qty], [StockID]) VALUES (#" & _
dteStartDate & "#", lngQty & ", '" & strItemID & "')"

StockID is a number data type in the table, not sure if that's the cause.
Jun 10 '09 #4
ADezii
8,834 Expert 8TB
@KashMarsh
Try the following changes:
Expand|Select|Wrap|Line Numbers
  1. Dim lngItemID As Long
  2.  
  3. CurrentDb.Execute "INSERT INTO tShipDetails ([StartDate], [Qty], [StockID]) VALUES (#" & _
  4. dteStartDate & "#", lngQty, lngItemID & ")"
Jun 11 '09 #5
OldBirdman
675 512MB
This is not directly addressing the issue but as a comment: OnMouseUp is not the same as OnClick.

I hate changing from mouse to keyboard to mouse to keyboard, and once on either, will stay as long as I can. I know the keyboard replacements for mouse-driven items. Using Tab, F4, and Arrow Keys I can dropdown and select from a combobox without triggering the MouseUp event. Therefore, this code would not work for me, and I would conclude the program had a bug.

MouseUp is valid at times, but I PERSONALLY don't feel that this is such a time.
Jun 11 '09 #6
ADezii,
First of all, thank you for your help.

I have made the changes and still get the error on this part of the code:
Expand|Select|Wrap|Line Numbers
  1. CurrentDb.Execute "INSERT INTO tShipDetails ([StartDate], [Qty], [StockID]) VALUES (#" & _
  2.   dteStartDate & "#", lngQty & ", '" & lngItemID & "')"
These two lines are highlighted in yellow but the arrow is pointing to the line starting with dteStartDate. Also, is there something I should check? Such as if I have something missing or wrong versions of any Jet Engines or anything of that sort?

Here is the entire code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAddShipSched_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. Dim dteStartDate As Date
  3. Dim intFreq As Integer
  4. Dim lngQty As Long
  5. Dim lngItemID As Long
  6. Dim i As Integer
  7.  
  8. dteStartDate = Me.txtShipStart
  9.  
  10. intFreq = Me.txtShipDur
  11. lngQty = Me.txtQty
  12. lngItemID = Me.cboStockID
  13.  
  14. i = 0
  15.  
  16. Do Until i = intFreq
  17.   CurrentDb.Execute "INSERT INTO tShipDetails ([StartDate], [Qty], [StockID]) VALUES (#" & _
  18.   dteStartDate & "#", lngQty & ", '" & lngItemID & "')"
  19.     dteStartDate = DateAdd("m", 1, dteStartDate)
  20. i = i + 1
  21. Loop
  22.  
  23. End Sub
Also, OldBirdMan, thanks for your input. It's good to keep your comments in mind for users that are more keyboard oriented. The reason why I prefer to use onMouseUP is because most users I work with are heavy mouse users and prefer to click things. Since they are heavy mouse users, they know that they can cancel a click by moving off the button before completing the click. When I had code on the "on click" event, their way of canceling the click action didn't work. When I changed it to onMouseUp, cancelling a click worked as they expected. I have written confirmation prompts following onClick events but have stopped since using the onMouseUp and now only use it to confirm long or permanent actions.
Jun 11 '09 #7
ADezii,
My apologies, I did not copy the code correctly on the 2nd fix. Here is the code snippet as modified:
Expand|Select|Wrap|Line Numbers
  1. CurrentDb.Execute "INSERT INTO tShipDetails ([StartDate], [Qty], [StockID]) VALUES (#" & _
  2.   dteStartDate & "#", lngQty, lngItemID & ")"
  3.     dteStartDate = DateAdd("m", 1, dteStartDate)
I now get the error:
"The expression On Mouse Up you entered as the event property setting produced the following error: Wrong number of arguments or invalid property assignment."
Jun 11 '09 #8
ADezii
8,834 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. CurrentDb.Execute "INSERT INTO tShipDetails ([StartDate], [Qty], [StockID]) VALUES (#" & _
  2.                    dteStartDate & "#, " & _
  3.                    lngQty & ", " & lngItemID & ")"
Jun 12 '09 #9
ADezii,
Much appreciated. Is there any reference I can consult that goes over this kind of syntax for Access VBA in detail? Particularly what order to place the variables and the characters used for each data type. I see that using a text data type is entered differently vs a number vs a date. This is similar to SQL but not exactly.
You have been most helpful, the changes worked beautifully!
Jun 12 '09 #10
ADezii
8,834 Expert 8TB
@KashMarsh
I'm glad everything worked out for you. As far as References go, they are too numerous to mention, but you may wish to consider an introductory Book on SQL (Beginner to Intermediate Level).
Jun 12 '09 #11
NeoPa
32,556 Expert Mod 16PB
The original problem (a quite common one) was where the data was included in the string and where that ended.

I would also point out that printing a date exactly as it comes is not 100% reliable. For details see Literal DateTimes and Their Delimiters (#).

Welcome to Bytes!
Jun 17 '09 #12

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

Similar topics

2
by: JMCN | last post by:
hi i need some advice on whether if it would be better to use an append query or an update query. here is the situation, i have linked another database table to my current database. then i...
2
by: Danny | last post by:
I want to extract a subset of fields from one table into another the master table has many fields the subset has about half, but still many. Is there a way I can just append the master into the...
1
by: David Barger | last post by:
Greetings, It appears that an Append Query I run in Access XP is randomly failing to append a field. I have payroll data being entered into a payroll database. This data is exported daily to...
5
by: Wayne Wengert | last post by:
I am using VB ASP.NET. In my page I convert an uploaded XML file to a dataset as follows: Dim ds1 As DataSet = New DataSet ds1.ReadXml(strPathName, XmlReadMode.Auto) Now I want to append...
6
by: Reggie | last post by:
Hi and TIA. I have an OleDB connection to an Access db which I connect to and place data from a table into a recordset. What I want to do is copy this recordset to an SQL table. I have no...
4
by: robboll | last post by:
When I try to use an append query from an oracle link it takes forever. I am exploring the idea of doing an append action using a pass-through query. If I have an Oracle ODBC connection to...
3
by: wvmbark | last post by:
First time poster... I just found this forum and it appears there's plenty of people here that could make short work of problem that's been driving me absolutely bonkers for months. Every day we...
4
by: ray well | last post by:
i need to copy in code a table from a legacy dbase III file into an access mdb file. both source and destination tables have the same table names, field names, field types, & field sizes. the...
4
by: dougmeece | last post by:
Morning Everyone... I have a table that needs to be append to and also updated. All the fields in the table are populated with data from the text boxes and combo boxes on a form. The Date...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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,...

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.