Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613484
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:33:27+00:00 2026-05-22T01:33:27+00:00

I have created this function GetSubName that I need to return the name that

  • 0

I have created this function GetSubName that I need to return the name that is saves from a pull down box. It does this just fine as the dialog boxes I have used shows that it sets the variable correctly. The problem is that when the SQL below runs in a query I get the error: “Undefined function ‘GetSubName’ in expression.” I am new to VBA so any help would be much appreciated.

Here is the code:

Option Compare Database
Option Explicit
Private stSubName As String

Private Sub Command2_Click()
On Error GoTo Err_Command2_Click
Dim stDocName As String
Dim stSubName As String

SubcontractorCombo.SetFocus
stSubName = SubcontractorCombo.SelText
'Confirm that stSubName variable is holding correct value'
MsgBox "Name of Subcontractor Selected is " & stSubName

SetSubName stSubName
GetSubName

DoCmd.Close

stDocName = "Summary Asphalt Production for Subcontractor"
DoCmd.OpenQuery stDocName

Exit_Command2_Click:
Exit Sub

Err_Command2_Click:
MsgBox Err.Description
Resume Exit_Command2_Click

End Sub

Public Sub SetSubName(Value As String)
'Set the module variable to be the value passed in from externally'
stSubName = Value
End Sub

Public Function GetSubName() As String
'Returns the value of the module variable'
GetSubName = stSubName
'MsgBox "GetSubName Variable is " & stSubName'

End Function

And here is my SQL from inside of Access 2007:

SELECT DISTINCTROW Subs.Subcontractor, Counties.County, Projects.ContractID,
Sum(Project_Items.USTons) AS SumOfUSTons, Projects.PlanQuantity,
Max(Project_Items.EstDate) AS MaxOfEstDate, Project_Items.Sub
FROM Counties INNER JOIN (Subs INNER JOIN (Projects INNER JOIN Project_Items ON  
Projects.ContractID = Project_Items.ProjectID) ON Subs.VendID = Project_Items.Sub) ON 
Counties.ID = Project_Items.County
WHERE (((Projects.Completed)<>True) AND ((Subs.Subcontractor)=GetSubName()))
GROUP BY Subs.Subcontractor, Counties.County, Projects.ContractID,   
Projects.PlanQuantity, Project_Items.Sub;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:33:27+00:00Added an answer on May 22, 2026 at 1:33 am

    The reason the functions are not recognized is that you haven’t fully specified the name. A public function in a form module needs to be specified with the form name:

      Forms!MyForm.GetSubName()
    

    But this is the wrong approach, and your code is way too convoluted. You can access the value of the combo box in your query directly:

      Forms!MyForm!SubcontractorCombo
    

    Now, the fact that you’re using .SelText suggests to me either that you’re doing something very very tricky, or you have your combo box set up wrong. Combo boxes can have a found field and a display value, such that a list of employees might display the employee LastName/FirstName while the combo box actually has as its bound field the EmployeeID.

    If your combo box has a hidden bound field, but you want the displayed value, you don’t need to use .SelText — just use the appropriate .Column() of the combo box:

      Forms!MyForm!SubcontractorCombo.Column(1)
    

    (the column count is zero-based, so the hidden column would be column 0, assuming it’s the first column that is hidden)

    Also, there’s an issue that if the user selects PART of the text in the combo box, you’d have an incomplete match, so you really don’t want to use .SelText at all.

    So, the WHERE clause of your SQL would end up being this (assuming I’ve diagnosed everything correctly):

      WHERE Projects.Completed<>True 
         AND Subs.Subcontractor=Forms!MyForm!SubcontractorCombo.Column(1)
    

    …and you can lose all of the marked code:

    Option Compare Database
    Option Explicit
    <strike>Private stSubName As String</strike>
    
    Private Sub Command2_Click()
    On Error GoTo Err_Command2_Click
      Dim stDocName As String
      Dim stSubName As String
    
      SubcontractorCombo.SetFocus
      <strike>stSubName = SubcontractorCombo.SelText</strike>
      'Confirm that stSubName variable is holding correct value'
      <strike>MsgBox "Name of Subcontractor Selected is " & stSubName</strike>
    
      <strike>SetSubName stSubName</strike>
      <strike>GetSubName</strike>
    
      DoCmd.Close
    
      stDocName = "Summary Asphalt Production for Subcontractor"
      DoCmd.OpenQuery stDocName
    
    Exit_Command2_Click:
      Exit Sub
    
    Err_Command2_Click:
      MsgBox Err.Description
      Resume Exit_Command2_Click
    
    End Sub
    
    <strike>Public Sub SetSubName(Value As String)
      'Set the module variable to be the value passed in from externally'
      stSubName = Value
    End Sub</strike>
    
    <strike>Public Function GetSubName() As String
      'Returns the value of the module variable'
      GetSubName = stSubName
      'MsgBox "GetSubName Variable is " & stSubName'    
    End Function</strike>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I have one JavaScript that creates rows in a table like this: function
I have created an web service I need to publish this service. I need
I have created an Excel 2003 add-in that uses the CLR 2.0 and this
I have created a basic WCF service in IIS. I am aware that this
I have an ISAm table in mySql that was created similar to this: create
Ok so I have this regex that I created and it works fine in
I have a table that is created in a DataList in ASP.Net. This table
I have a report that I created with Crystal Reports 2008. This report uses
Scenario: I have a document I created using LaTeX (my resume in this case),
This is what I currently have: CREATE OR REPLACE TRIGGER MYTRIGGER AFTER INSERT ON

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.