I'm new at adding VBA to Access 2007 and could really use some help.
I have two forms used to gather data to go into a datasheet subform. The following code on the second of the two forms populates the subform: - Private Sub OK_Click()
-
-
If ([txt_Act_Qty] > [txt_In_Stock]) Then
-
MsgBox "Sales Quantity cannot exceed Quantity In Stock", vbOKOnly
-
[txt_Act_Qty].SetFocus
-
Exit Sub
-
Else
-
End If
-
-
Forms![frm_New_Sale].[sfrm_Sales_Details]![InvOrderNum] = [txtInvOrderNO]
-
Forms![frm_New_Sale].[sfrm_Sales_Details]![InvDetailID] = [txtDetailID]
-
Forms![frm_New_Sale].[sfrm_Sales_Details]![Item] = [txtItem]
-
Forms![frm_New_Sale].[sfrm_Sales_Details]![ItemCost] = [txtItemCost]
-
Forms![frm_New_Sale].[sfrm_Sales_Details]![ItemQty] = [txt_Act_Qty]
-
Forms![frm_New_Sale].[sfrm_Sales_Details]![SellingPrice] = [txt_Act_Price]
-
DoCmd.RunCommand acCmdSaveRecord
-
DoCmd.GoToRecord , , acNewRecord
-
DoCmd.Close
-
-
End Sub
After populating the row I want to move to the next record to allow the user to insert another item. However, when Access tries to go to the next record I get "Runtime Error 2105 You can't go to the specified record".
Any advice would be great.
Thanks in advance.
11 14577
If I'm reading this right, then it's trying to save/advance the records of the main form, rather than telling the subform to save/advance. If the main forms are unbound to any tables, then you'll get that error.
Try specifying the subform name in the docmd lines, and see if that helps.
You may want to make your Base Code a little more organized and efficient: -
If ([txt_Act_Qty] > [txt_In_Stock]) Then
-
MsgBox "Sales Quantity cannot exceed Quantity In Stock", vbOKOnly
-
[txt_Act_Qty].SetFocus
-
Exit Sub
-
Else
-
With Forms![frm_New_Sale].[sfrm_Sales_Details]
-
![InvOrderNum] = [txtInvOrderNO]
-
![InvDetailID] = [txtDetailID]
-
![Item] = [txtItem]
-
![ItemCost] = [txtItemCost]
-
![ItemQty] = [txt_Act_Qty]
-
![SellingPrice] = [txt_Act_Price]
-
End With
-
'... continue processing
-
End If
-
Megalog and ADezii, thank you both for your replies. Adezii, your advice worked great and the code is a lot cleaner. Megalog, my main form and subform are bound to tables and the subform is joined to the mainform on the Invoice Number. I've tried to set focus to the subform first but I get an error stating the method isn't supported. How can I make sure the DoCmd lines run on the subform? - DoCmd.RunCommand acCmdSaveRecord
-
DoCmd.GoToRecord , , acNewRecord
Try:- Set Focus to the Sub-Form Control
- Save Record in Sub-Form
- Save the Record with SendKeys
- Close the Form
-
Forms![frm_New_Sale].[sfrm_Sales_Details].SetFocus
-
DoCmd.RunCommand acCmdSaveRecord
-
SendKeys "{^}+"
-
DoCmd.Close
Wow! It worked!
However, the record saves to the table and disappears from view. I need the record to remain in the subform until all of the items are added to the invoice. Maybe I should use an unbound form and use code to save the data? Any ideas?
Here's a recap of the code: - Private Sub OK_Click()
-
-
If ([txt_Act_Qty] > [txt_In_Stock]) Then
-
MsgBox "Sales Quantity cannot exceed Quantity In Stock", vbOKOnly
-
[txt_Act_Qty].SetFocus
-
Exit Sub
-
-
Else
-
With Forms![frm_New_Sale].[sfrm_Sales_Details]
-
![InvOrderNum] = [txtInvOrderNO]
-
![InvDetailID] = [txtDetailID]
-
![Item] = [txtItem]
-
![ItemCost] = [txtItemCost]
-
![ItemQty] = [txt_Act_Qty]
-
![SellingPrice] = [txt_Act_Price]
-
End With
-
-
Forms![frm_New_Sale].[sfrm_Sales_Details].SetFocus
-
DoCmd.RunCommand acCmdSaveRecord
-
SendKeys "{^}+"
-
DoCmd.Close
-
-
End If
-
End Sub
Wow! It worked!
However, the record saves to the table and disappears from view. I need the record to remain in the subform until all of the items are added to the invoice. Maybe I should use an unbound form and use code to save the data? Any ideas?
Here's a recap of the code: - Private Sub OK_Click()
-
-
If ([txt_Act_Qty] > [txt_In_Stock]) Then
-
MsgBox "Sales Quantity cannot exceed Quantity In Stock", vbOKOnly
-
[txt_Act_Qty].SetFocus
-
Exit Sub
-
-
Else
-
With Forms![frm_New_Sale].[sfrm_Sales_Details]
-
![InvOrderNum] = [txtInvOrderNO]
-
![InvDetailID] = [txtDetailID]
-
![Item] = [txtItem]
-
![ItemCost] = [txtItemCost]
-
![ItemQty] = [txt_Act_Qty]
-
![SellingPrice] = [txt_Act_Price]
-
End With
-
-
Forms![frm_New_Sale].[sfrm_Sales_Details].SetFocus
-
DoCmd.RunCommand acCmdSaveRecord
-
SendKeys "{^}+"
-
DoCmd.Close
-
-
End If
-
End Sub
Try a Requery on the Sub-Form P.S. - Are you closing the Form in Line #21?
I have a sales main form that contains details about the overall sale (Invoice Number, customerID etc...). The main form has a bound subform that holds information about each item added to the sale. The user clicks a button that opens a query form to search Inventory Orders for the items thay want to add. The user double clicks the record they want to add and it transfers all of the data from the selected row to a middle form that forces the user to enter a quantity and price. The user clicks on the OK button that triggers the following code: - Private Sub OK_Click()
-
-
If ([txt_Act_Qty] > [txt_In_Stock]) Then
-
MsgBox "Sales Quantity cannot exceed Quantity In Stock", vbOKOnly
-
[txt_Act_Qty].SetFocus
-
Exit Sub
-
-
Else
-
With Forms![frm_New_Sale].[sfrm_Sales_Details]
-
![InvOrderNum] = [txtInvOrderNO]
-
![InvDetailID] = [txtDetailID]
-
![Item] = [txtItem]
-
![ItemCost] = [txtItemCost]
-
![ItemQty] = [txt_Act_Qty]
-
![SellingPrice] = [txt_Act_Price]
-
End With
-
-
Forms![frm_New_Sale].[sfrm_Sales_Details].SetFocus
-
DoCmd.RunCommand acCmdSaveRecord
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "+{ENTER}"
-
'SendKeys "{^}+"
-
'DoCmd.GoToRecord , , acLast
-
DoCmd.Close
-
-
End If
-
End Sub
I had the subform configured for data entry. After changing data entry back to No, the process is saving the row to the table and the items remain on the subform. However, sometime after it saves the row, I get a "Ding" like there was an error and it doesn't advance to the next row.
There are no required fields except the primary key and all of the fields are filled in.
Any Ideas
I have a sales main form that contains details about the overall sale (Invoice Number, customerID etc...). The main form has a bound subform that holds information about each item added to the sale. The user clicks a button that opens a query form to search Inventory Orders for the items thay want to add. The user double clicks the record they want to add and it transfers all of the data from the selected row to a middle form that forces the user to enter a quantity and price. The user clicks on the OK button that triggers the following code: - Private Sub OK_Click()
-
-
If ([txt_Act_Qty] > [txt_In_Stock]) Then
-
MsgBox "Sales Quantity cannot exceed Quantity In Stock", vbOKOnly
-
[txt_Act_Qty].SetFocus
-
Exit Sub
-
-
Else
-
With Forms![frm_New_Sale].[sfrm_Sales_Details]
-
![InvOrderNum] = [txtInvOrderNO]
-
![InvDetailID] = [txtDetailID]
-
![Item] = [txtItem]
-
![ItemCost] = [txtItemCost]
-
![ItemQty] = [txt_Act_Qty]
-
![SellingPrice] = [txt_Act_Price]
-
End With
-
-
Forms![frm_New_Sale].[sfrm_Sales_Details].SetFocus
-
DoCmd.RunCommand acCmdSaveRecord
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "(TAB)"
-
SendKeys "+{ENTER}"
-
'SendKeys "{^}+"
-
'DoCmd.GoToRecord , , acLast
-
DoCmd.Close
-
-
End If
-
End Sub
I had the subform configured for data entry. After changing data entry back to No, the process is saving the row to the table and the items remain on the subform. However, sometime after it saves the row, I get a "Ding" like there was an error and it doesn't advance to the next row.
There are no required fields except the primary key and all of the fields are filled in.
Any Ideas
None off the top of my head. I am going on Vacation shortly and will be back on the 18th. If you like, assuming this problem has not been resolved, you can send me the Database at that time and I would be happy to have a look at it.
Try: - Private Sub OK_Click()
-
If ([txt_Act_Qty] > [txt_In_Stock]) Then
-
..MsgBox "Sales Quantity cannot exceed Quantity In Stock", vbOKOnly
-
..[txt_Act_Qty].SetFocus
-
..Exit Sub
-
-
Else
-
..With Forms![frm_New_Sale].[sfrm_Sales_Details]
-
....![InvOrderNum] = [txtInvOrderNO]
-
....![InvDetailID] = [txtDetailID]
-
....![Item] = [txtItem]
-
....![ItemCost] = [txtItemCost]
-
....![ItemQty] = [txt_Act_Qty]
-
....![SellingPrice] = [txt_Act_Price]
-
..End With
-
..Forms![frm_New_Sale].[sfrm_Sales_Details].SetFocus
-
..DoCmd.RunCommand acCmdSaveRecord
-
..DoCmd.GoToRecord , , acNewRec
-
..SendKeys "{TAB 2}"
-
..DoCmd.Close acForm, "frm_Qty_Price_Input", acSaveYes
-
End If
-
End Sub
The subform still wouldn't advance to a new record. I would get an error Ding sound each time it ran so I figured it was erroring out saving the record. Then I remembered that Im enforcing referential integrity on the relationship. Since I don't know how to step through my code I tried saving the parent record first and it worked. I added the first two lines below: - Forms![frm_New_Sale].SetFocus
-
DoCmd.RunCommand acCmdSaveRecord
-
-
Forms![frm_New_Sale].[sfrm_Sales_Details].SetFocus
-
DoCmd.RunCommand acCmdSaveRecord
-
DoCmd.GoToRecord , , acNewRec
Thanks for all of your help.
The subform still wouldn't advance to a new record. I would get an error Ding sound each time it ran so I figured it was erroring out saving the record. Then I remembered that Im enforcing referential integrity on the relationship. Since I don't know how to step through my code I tried saving the parent record first and it worked. I added the first two lines below: - Forms![frm_New_Sale].SetFocus
-
DoCmd.RunCommand acCmdSaveRecord
-
-
Forms![frm_New_Sale].[sfrm_Sales_Details].SetFocus
-
DoCmd.RunCommand acCmdSaveRecord
-
DoCmd.GoToRecord , , acNewRec
Thanks for all of your help.
Anytime, that's what we are all here for.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: swathky |
last post by:
I've tried mutiple things but no go -- (sorry this is so long)
I'm collecting a 5 digit number in an input box function and all works
fine until a number is passed that doesn't exist in the...
|
by: samotek |
last post by:
Can i avoid the appearance of the Run time error 2105?
I have a function that performs well,but at the end
the Run time error 2105 appears :" You cant go to the
specified record"
However...
|
by: g_man |
last post by:
I am trying trap Runtime error 3022 (duplicates) in the click event of
a command button that closes the form. I have code in the Form_Error
event that does a good job of providing a more meaningful...
|
by: David01 |
last post by:
When I run some code behind a button i get a runtime error message. The strange thing is that the code run perfectly some months ago. Can some one help me !
Some additional information: The company...
|
by: Gilberto |
last post by:
Hello, in several forms i have a NEXT, PREVIOUS and SAVE command buttons which were working perfectly...no errors showing...nothing!!! i had to convert my DB to 2003 so i could create the .mde. I did...
|
by: rsmccli |
last post by:
Using AC2002
Hello. I am working with an existing DB that has "Add New" command buttons on two forms.
When I, an Admin, click the buttons, they work properly, and a new, blank form is created,...
|
by: Jim Armstrong |
last post by:
Hello all -
This is driving me crazy. I have a table called tblClients - very
simple, has the following fields:
taxID (PK)
ClientName
SalesName
The main form of my application allows a...
|
by: dpsathas |
last post by:
Hi everybody
I have a database created in access 2007. In one form I created a button with the following VBA code (on click event)
DoCmd.GoToRecord , , acNewRec
A_S = Nz(DMax(“A_S”, “Table1”))...
|
by: simulationguy |
last post by:
I have a database the runs fine in Access 2003 but this routine crashes with error 2105 "You can't go to the specified record" in Access 2007 on the last line !itemNumber.SetFocus
Any idea why...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |