I have an access database that uses a subform to display records, one column of which is charge data (currency). My users need to verify the charges against the paper form they are scanned from, so I would like to allow them to select a subset of these charges (shift click) and sum them into a message box to verify page by page that the records are correct, or find incorrect records. I found the following vba code and added it to the db
On the subform to set the record set - Private Sub Form_Click()
-
-
CurrentSelectionTop = Me.SelTop
-
CurrentSelectionHeight = Me.SelHeight
-
-
End Sub
On the main form to total the selected records and return the sum to a msgbox(error handling commented out for dev purposes): - Private Sub btnRecordSum_Click()
-
'On Error GoTo Err_btnRecordSum_Click
-
-
Dim curTotal As Currency
-
Dim lngFirstRec As Long
-
Dim lngNRecs As Long
-
-
With Me![SutaList].Form
-
'Forms![frmMaster]![frmCar]
-
-
lngFirstRec = CurrentSelectionTop
-
lngNRecs = CurrentSelectionHeight
-
-
If lngNRecs > 0 Then
-
-
With .RecordsetClone
-
-
.AbsolutePosition = lngFirstRec - 1
-
-
While lngNRecs > 0
-
curTotal = curTotal + Nz(!Amount, 0)
-
lngNRecs = lngNRecs - 1
-
If lngNRecs > 0 Then
-
.MoveNext
-
End If
-
Wend
-
-
End With
-
-
End If
-
-
End With
-
'Exit_btnRecordSum_Click:
-
' Exit Sub
-
-
'Err_btnRecordSum_Click:
-
' MsgBox Err.Description
-
' Resume Exit_btnRecordSum_Click
-
-
MsgBox "The total is " & curTotal
-
End Sub
My problem is that when I click the btnRecordSum, the selected rows become unselected, and the msgbox returns an amount of 0 which is correct for no selection. Am I missing something obvious here? How do I retain the selection after the subform loses focus?
@thayes5150
This theoretically may work, and initial tests indicates that it may. - Set the Key Preview Property of the 'Main' Form to No.
- Set the Key Preview Property of the 'Sub-Form' to Yes.
- Write code in the KeyDown() Event of the Sub-Form to Capture the ALT+S Keystroke combination (Not S). The code is as follows:
- Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
-
If (Shift And acAltMask) > 0 And (KeyCode = vbKeyS) Then
-
'...ALT+S pressed, Msgbox and code processing here
-
End If
-
End Sub
- Have the User select the Records, then press the ALT+S Key combination in order to execute the code contained within.
- Let me know how you make out.
10 7062
Sorry, Access 2003, Datasheet form
@thayes5150
I don't think that you can maintain the Records Selection once you leave the Sub-Form, but what you can do is: - Include a Field named [Sum], [Yes/No displayed as Check Box], in the Record Source for your Sub-Form.
- Include the Field in the Sub-Form.
- Have the User select as many Fields as he/she wants on the Sub-Form to be totalled.
- Run code similar to the following (Order Details Extended Query of Northwind) to generate a Total for the Records selected.
- MsgBox "Total Price for all selected Items: " & _
-
Format$(Nz(DSum("[ExtendedPrice]", "Order Details Extended", "[Sum]=True")), "Currency")
- Reset all [Sum] Fields to False.
- This theory has been tested and does work.
Unfortunately the data in question is sometimes 20 -30 pages with 50 records on each page and I don't know of a way to mass select the check boxes (as you can drag select records in a data sheet) This means they would need to check 50 boxes, then sum, then check 50 more etc for all 20 -30 pages. Is it possible to add a keypress event, maybe on the S key so when they select the records and press S it pops the sum message?
@thayes5150
This theoretically may work, and initial tests indicates that it may. - Set the Key Preview Property of the 'Main' Form to No.
- Set the Key Preview Property of the 'Sub-Form' to Yes.
- Write code in the KeyDown() Event of the Sub-Form to Capture the ALT+S Keystroke combination (Not S). The code is as follows:
- Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
-
If (Shift And acAltMask) > 0 And (KeyCode = vbKeyS) Then
-
'...ALT+S pressed, Msgbox and code processing here
-
End If
-
End Sub
- Have the User select the Records, then press the ALT+S Key combination in order to execute the code contained within.
- Let me know how you make out.
First I want to thank you for all of your help so far, The code you provided seems to work fine, and fires the btnRecordSum_Click code I included before, but the total still comes up as zero, despite the selected records. If I uncomment the error code the message box does not pop, which makes me think there is a problem in the code that sums the records, but it is also not popping the Error message box, so I don't know where the issue lies. I think we are very close on this. Any ideas?
@thayes5150
I've actually got the code working quite well. I based my Demo on the Orders Subform in the Orders Table of the Northwind Sample Database. It will Total the [ExtendedPrice] Field for all those Records selected in the Subform. I'll post the code and also Attach the Demo Database in case you have any doubts. Let me know how you make out on this. - Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
-
If (Shift And acAltMask) > 0 And (KeyCode = vbKeyS) Then
-
Dim curTotal As Currency
-
Dim lngFirstRec As Long
-
Dim lngNRecs As Long
-
-
lngFirstRec = Me.SelTop
-
lngNRecs = Me.SelHeight
-
-
If lngNRecs > 0 Then
-
Me.RecordsetClone.AbsolutePosition = lngFirstRec - 1
-
With Me.RecordsetClone
-
.AbsolutePosition = lngFirstRec - 1
-
-
While lngNRecs > 0
-
curTotal = curTotal + Nz(![ExtendedPrice], 0)
-
lngNRecs = lngNRecs - 1
-
If lngNRecs > 0 Then
-
.MoveNext
-
End If
-
Wend
-
End With
-
End If
-
-
MsgBox "Total Extended Price: " & Format$(curTotal, "Currency")
-
End If
-
End Sub
I actually just figured it out as I received your e-mail. Stupid copy/paste error on my part - when I brought the line curTotal = curTotal + Nz(!Amount, 0) into my code I forgot to change the field name from Amount to TtlChg, updated that and it works like a champ. Thanks again for all of the help.
NeoPa 32,556
Expert Mod 16PB
The deselection problem is due to moving away from your subform (I'm pretty sure). A simple way to get around this might be to trigger the code from this subform (rather than from the main form as originally). The keystroke approach does this of course, but is possibly more complicated than you need.
If you had a Command Button on the subform (in a Header or Footer section) then I expect your original concept should work fine.
Of course, if you particularly like the keystroke method, or even just like knowing how that can be implemented, well then ADezii's response gives you that too :)
Thanks again everybody for all the help - The subform is really just a Datasheet view of the underlying table and so Buttons on the subform are not an option. Also I kind of like the Alt -S option. It's kind of elegant.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: YFS DBA |
last post by:
How do I use VBA to insert a *new* record into a subform?
I have a master form with client information, and a sub form with billing
information. I want to click on a button ("Add Data") and have...
|
by: damjanu |
last post by:
Hi All;
I need little help.
I have a datasheet form.
I allow user to do 'filter by selection'.
My form contains a column with values.
As user changes selections, I want to
calculate totals....
|
by: Uwe Range |
last post by:
Hi to all,
I am displaying a list of records in a subform which is embedded in a
popup main form (in order to ensure that users close the form when
leaving it).
It seems to be impossible to...
|
by: Steve |
last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I
have code that sets the default value of each field to its current value. For a
new record, I can put the focus in any...
|
by: swingingming |
last post by:
Hi, in the NorthWind sample database, when clicking on the next
navigation button on the new order record with nothing on the subform
(order details), we got an order with nothing ordered. How can...
|
by: MLH |
last post by:
When the vehicle entry form (frmVehicleEntryForm) first opens, the
additional owner SubForm control (frmAddnlOwnrListSubForm) is
enabled. You can click on it and it will accept the focus. But after...
|
by: angi35 |
last post by:
I hope this is an easy question for someone out there. In Access 2000…I have a MainForm with a tab control (MAIN TABS) with 7 tabs.
Within each tab is a SubForm.
Within each SubForm is a tab...
|
by: Dave |
last post by:
Access 2003
I have an unbound search form, which shows filtered search records in
it’s sub-form.
On the unbound search form I wish to place a button, which will close
off the search form, and...
|
by: bibek24 |
last post by:
I have a continious subform which is only visible when the main form gets open.
The subform has a text box which is locked for the user.In order to enter something into it, a user has to click a...
|
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: 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...
|
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: 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...
|
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: 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: 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: 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,...
| |