473,396 Members | 1,996 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,396 software developers and data experts.

Referencing subform during module code


I have a form that contains two subforms. Both of these subforms use
the same function.

Rather than repeating the code, I want to be able to pass the form name
to the code. Right now, its a module, but I guess it could go at the
form level. I rather keep it in a module in case I ever have other
forms that would use the code.

The code is :

Public Function LookUpZip(strZip As String, strFormName)
Dim db As DAO.Database, rs As DAO.Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT ZIP, CITY, STATE FROM tblZipCodes
WHERE ZIP = '" & strZip & "'")

If rs.RecordCount = 0 Then
MsgBox "No Matching Zip Codes Could Be Found. Please enter
information manually."
Forms(strFormName).txtContactCity.SetFocus
Else
Forms(strFormName).txtContactCity = rs("City")
Forms(strFormName).txtContactStateOrProvince = rs("State")
End If

Set rs = Nothing
Set db = Nothing
End Function

This would work at the form level, but not at the subform. I need to
reference the subform.

Could someone please help me out?

Thanks

Feb 12 '06 #1
3 2081
BerkshireGuy wrote in message
<11**********************@g47g2000cwa.googlegroups .com> :
I have a form that contains two subforms. Both of these subforms use
the same function.

Rather than repeating the code, I want to be able to pass the form name
to the code. Right now, its a module, but I guess it could go at the
form level. I rather keep it in a module in case I ever have other
forms that would use the code.

The code is :

Public Function LookUpZip(strZip As String, strFormName)
Dim db As DAO.Database, rs As DAO.Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT ZIP, CITY, STATE FROM tblZipCodes
WHERE ZIP = '" & strZip & "'")

If rs.RecordCount = 0 Then
MsgBox "No Matching Zip Codes Could Be Found. Please enter
information manually."
Forms(strFormName).txtContactCity.SetFocus
Else
Forms(strFormName).txtContactCity = rs("City")
Forms(strFormName).txtContactStateOrProvince = rs("State")
End If

Set rs = Nothing
Set db = Nothing
End Function

This would work at the form level, but not at the subform. I need to
reference the subform.

Could someone please help me out?

Thanks


In stead of passing the form name - which might give a bit more
challenges, pass the form, and refer through it.

Public Function LookUpZip(strZip As String, frm as form)
....
frm!txtContactCity = rs("City")
....

Then call it with

Call LookUpZip(strYourZip, Me)

Also - since you're not returning anything, convert to a sub?

--
Roy-Vidar
Feb 12 '06 #2
BerkshireGuy wrote:
I have a form that contains two subforms. Both of these subforms use
the same function.

Rather than repeating the code, I want to be able to pass the form name
to the code. Right now, its a module, but I guess it could go at the
form level. I rather keep it in a module in case I ever have other
forms that would use the code.

The code is :

Public Function LookUpZip(strZip As String, strFormName)
Dim db As DAO.Database, rs As DAO.Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT ZIP, CITY, STATE FROM tblZipCodes
WHERE ZIP = '" & strZip & "'")

If rs.RecordCount = 0 Then
MsgBox "No Matching Zip Codes Could Be Found. Please enter
information manually."
Forms(strFormName).txtContactCity.SetFocus
Else
Forms(strFormName).txtContactCity = rs("City")
Forms(strFormName).txtContactStateOrProvince = rs("State")
End If

Set rs = Nothing
Set db = Nothing
End Function

This would work at the form level, but not at the subform. I need to
reference the subform.

Could someone please help me out?

Thanks

You might be better off passing the form instead of the name. Ex:
LookUpZip "12345", Me

You could have some code to determine if the form is a subform
Public Function IsSubform(frm As Form)
Dim strName As String
On Error Resume Next
' This action will trigger a runtime
' error if the form isn't loaded as a subform.
strName = frm.Parent.name
IsSubform = (Err = 0)
End Function

Public Sub LookUpZip(strZip As String, frm As Form)
Dim strName As String
Dim strSub As String
If IsSubform(frm) Then
strSub = frm.Name
strName = frm.Parent.Name
else
strName = frm.Name
endif
If strSub = "" Then
Forms(strName).txtFld = rs("City")
Forms(strName).txtState = rs("State")
Else
Forms(strName)(strSub).txtFld = rs("City")
Forms(strName)(strSub).txtState = rs("State")
Endif
End Sub
Feb 12 '06 #3
Any form used in anyway can be referenced as Form_formname if it has
its HasModule property set to true as in:
Form_formname.txtContactCity.Value=

Feb 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: William Wisnieski | last post by:
Hello Everyone, Access 2000 I have a main form with a continuous subform. On the main form I have a text box that references a field on the subform. What I'd like it to do is show the value...
4
by: CSDunn | last post by:
Hello, I have a combo box (Combo7) that needs to call a function during the After Update event of the combo box. The function resides in an Access 2000 ADP Module called MMAnswerData_code. The...
3
by: shumaker | last post by:
This code from the subform works for getting the value of a field of the main form named "WorkSheet": MsgBox Form_WorkSheet.Recordset.Fields("Clerk").Value Each record in the mainform datasheet...
4
by: mplogue | last post by:
I have a form (frmMain) with a subform (frmSub), each with enumerated fields of the same name (txt1, txt2, etc). I'm trying to make a function that will take the values for each field in frmMain,...
2
by: Smartin | last post by:
Using Access 97 I am trying to create a form that allows for user entry of search terms and displays a table of results. I thought I could accomplish this using a subform but it isn't quite...
2
by: Axel | last post by:
Hi, a question about something that seems very simple at first glance: is it possible to reference other controls of a subform in a query window without referencing through the parent form? I...
6
by: Mat | last post by:
Dear all, What I want to do is be able to use a string to refer to a control on a subform. IE: Forms!("Form1!form2!controlA").name or
9
by: Alan | last post by:
Hmmm, I'm not too good with the syntax of referencing a subreport. I have frmInvoice which has the invoice details (e.g. ProductCode, ProductCost etc) in the subform frmInvoiceDetails. I'm trying...
21
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main...
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: 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...
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
0
BarryA
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...
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
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
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
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.