Solved

Module Outside Form: Variable Form Name?

Posted on 2008-10-20
2
447 Views
Last Modified: 2013-11-28
I have several lines of VBA code that is used in 3 different forms. So in an attempt avoid code duplication, I put it into a subroutine in a module outside the forms. Unfortunately, I have to reference controls in the forms and now I find that "Me" only works in form modules.  Before I cut and paste this code back into the form subs, is there a way to make the form name variable? To complicate things a bit more, the name of one of the controls is variable. Here's the code:


Public Sub modSaveNewResults(intCount As Integer)
Dim rs As DAO.Recordset, j As Integer, query As String, rs1 As DAO.Recordset, rsForm As DAO.Recordset

query = "Select ALL SampleID, Analyte, Results, DateRun FROM tblResults WHERE SampleID=" & txtNumSearch & ";"

Set rs1 = CurrentDb.OpenRecordset("tblTempResults")
Set rs = CurrentDb.OpenRecordset(query)

  For j = 1 To intCount
    rs.MoveFirst
    AnalyteFound = False
      Do Until rs.EOF Or AnalyteFound = True
        If rs!Analyte = Me("Analyte " & j).Tag Then
          rs1.AddNew
          rs1!SampleID = Me!txtNumSearch
          rs1!Analyte = Me("Analyte" & j).Tag
          rs1!DateRun = Me!datAnRun
          rs1!Results = Me("Analyte" & j)
          rs1.Update
          AnalyteFound = True
        End If
      rs.MoveNext
      Loop
    Next j

rs.Close
rs1.Close
0
Comment
Question by:Gazaway
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 22761574
Try something like this:



Public Sub modSaveNewResults(intCount As Integer, FormName As String)
Dim rs As DAO.Recordset, j As Integer, query As String, rs1 As DAO.Recordset, rsForm As DAO.Recordset

query = "Select ALL SampleID, Analyte, Results, DateRun FROM tblResults WHERE SampleID=" & txtNumSearch & ";"

Set rs1 = CurrentDb.OpenRecordset("tblTempResults")
Set rs = CurrentDb.OpenRecordset(query)

  For j = 1 To intCount
    rs.MoveFirst
    AnalyteFound = False
      Do Until rs.EOF Or AnalyteFound = True
        If rs!Analyte = Forms(FormName).Controls("Analyte " & j).Tag Then
          rs1.AddNew
          rs1!SampleID = Forms(FormName).Controls("txtNumSearch").Value
          rs1!Analyte = Forms(FormName).Controls("Analyte" & j).Tag
          rs1!DateRun = Forms(FormName).Controls("datAnRun").Value
          rs1!Results = Forms(FormName).Controls("Analyte" & j).Value
          rs1.Update
          AnalyteFound = True
        End If
      rs.MoveNext
      Loop
    Next j

rs.Close
rs1.Close
0
 

Author Comment

by:Gazaway
ID: 22762157
That did the trick! Thanks for the prompt and complete reply!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

809 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