?
Solved

How to get a VBA code to find a subform

Posted on 2001-07-16
6
Medium Priority
?
592 Views
Last Modified: 2008-02-01
I have an Access db with an Invoice form that also had a detail subform.  The Invoice is an order form and the detail subform lists: quantity, description item ordered, and the inventory in stock for that item.  I have a cmd button that activates a module that is to update the inventory.  It is to find the description item ordered in the inventory table and subtract the quantity ordered from the stock.  I cannot get the code to find the subform no matter how I type it.  How do you code VBA in order to find fields in a subform?
0
Comment
Question by:tlwolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 19

Expert Comment

by:frankytee
ID: 6288399
try the following:

me.frmSub.form!fieldX

where frmSub is name of your subform, fieldX name of field
0
 
LVL 1

Accepted Solution

by:
ravl earned 1200 total points
ID: 6288419
To access a field on the sub-form from an event in the main form the format is:
Forms![MainFormName].[SubFormName]![SubFormFieldName]

So if your main form is called frmOrder and your sub-form is called frmDetail, then to reference the Quantity field in the sub-form you would type:
Forms![frmOrder].[frmDetail]![Quantity]
0
 
LVL 19

Expert Comment

by:frankytee
ID: 6288437
my code assumes you are trying to reference the subform from your main form.

me.frmSub.form!fieldX

if your name of your subform or field has spaces then you would have to enclose them in [ ] square brackets as ravl stated.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:hotbudare
ID: 6288481
I'd use code like this:

private sub test()
dim rs as dao.recordset
dim fld as dao.field

    set rs = forms("InvoiceFormName").Controls("SubformControlName").Form.Recordset
    do while not rs.eof
        for each fld in rs.fields
            debug.print fld.name & ":" & fld.value
        next fld
        rs.move.next
    loop
    set fld=nothing
    rs.close
    set rs=nothing
end  sub

HTH/EQTA
T.S.U. Mario Osorio
Punto Fijo, Falcon, Venezuela

0
 
LVL 12

Expert Comment

by:Paurths
ID: 6288525
hi tlwolf,

this might be usefull anytime working with subforms :

http://www.mvps.org/access/forms/frm0031.htm

cheers
Ricky
0
 

Author Comment

by:tlwolf
ID: 6296852
Here is my code.  It works properly except it will not move to the next record in the subform.  Can you make suggestions to help me complete this task?

Public Function UpdateInventory()

'declare variables and assign address to object variables

Dim cnnMSF As ADODB.Connection
Set cnnMSF = Application.CurrentProject.Connection

Dim Description As String, Quantity As Long, Stock As Long
Dim rstInventory As ADODB.Recordset, Invoice As Form
   
Set rstInventory = New ADODB.Recordset
Set rstInvoiceDetails = New ADODB.Recordset
Set Invoice = Application.Forms("Invoice")

'open recordset Inventory & InvoiceDetails, Invoice should be open already
rstInventory.Open Source:="Inventory", ActiveConnection:=cnnMSF, _
    CursorType:=adOpenForwardOnly, LockType:=adLockPessimistic
rstInvoiceDetails.Open Source:="InvoiceDetails", ActiveConnection:=cnnMSF, _
    CursorType:=adOpenForwardOnly, LockType:=adLockPessimistic
   
'assign values to controls on the form
Description = Forms![Invoice].[InvoiceDetails]![Description]
Quantity = Forms![Invoice].[InvoiceDetails]![Quantity]
Stock = Forms![Invoice].[InvoiceDetails]![Stock]

'search for the Description in Description field
rstInventory.Find criteria:="Description = '" & Description & "'"

'calculate Stock remaining after invoice purchase
Do Until rstInvoiceDetails.EOF
    If Stock >= Quantity Then
        rstInventory.Fields("Stock").Value = Stock - Quantity
    End If
    'move record pointer to the next record
    rstInvoiceDetails.MoveNext
Loop

'save changes to record
rstInventory.Update

'close the recordset
rstInventory.Close
Set rstInventory = Nothing        'disassociate object variable from object

'close the recordset
rstInvoiceDetails.Close
Set rstInvoiceDetails = Nothing        'disassociate object variable from object

End Function
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question