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

  • SEARCH
  • Home
  • 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 8799515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:20:41+00:00 2026-06-14T00:20:41+00:00

I am using vba code in onchange event of a combo field. the code

  • 0

I am using vba code in onchange event of a combo field. the code is as below

Dim db As Database
Dim rs As DAO.Recordset
Dim qrystr As String
Dim cond As String
Dim qrystr_CID As String

'cond = [Forms]![PharmDrug].[Commercial drugs subform2].Form.DrugCompanyName & vbNullString
cond = Me.DrugCompany & vbNullString

'MsgBox cond

Set db = CurrentDb

If cond = vbNullString Then
  ' do nothing
Else
  qrystr = "SELECT DrugCompanyID FROM [Drug Company] WHERE Name ='" & cond & "';"



Set rs = db.openrecordset(qrystr)


qrystr_CID = rs!DrugCompanyID



Me.DrugCompanyID = qrystr_CID

rs.Close
Set rs = Nothing
End If

this works fine but it gives error 3075 syntax error (missing operator) in query expression ‘Name= ‘Dr. Redy’s lab.’;’

that’s if the value in name field contain special characters like apostrophe etc. How can I get rid of this error ?

Please help me to solve this issue.

  • 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-06-14T00:20:42+00:00Added an answer on June 14, 2026 at 12:20 am

    Use a QueryDef for a parameter query.

    Dim qdf As DAO.QueryDef
    qrystr = "PARAMETERS which_name TEXT(255);" & vbCrLf & _
        "SELECT DrugCompanyID FROM [Drug Company] WHERE [Name] = [which_name];"
    Set qdf = db.CreateQueryDef(vbNullString, qrystr)
    qdf.Parameters("which_name") = cond
    Set rs = qdf.OpenRecordset
    

    You needn’t include quotes around the parameter in the SELECT statement, nor be concerned by any quotes contained within the text you supply for the parameter value. The db engine expects to receive text and will treat it as such.

    I also used square brackets around [Name] because it’s a reserved word. That doesn’t seem to have been a problem in this case; I bracket such names routinely as a precaution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had some existing VBA code that was building queries using string concatenation. I
I have been using VBA to some degree, using this code: Sub ExtractEmail() Dim
I'm trying to write a file using VBA. My code is below. It worked
I'm trying to access a query using VBA code. I think I've done it
I'm using Mercurial (specifically TortoiseHg on Windows) to do version control of VBA code.
I am looking to run SQL queries using VBA code in an Excel file.
How can I add a DateTimePicker to a User Form using VBA code. Similar
I am using the following VBA code to export PPT slides to images. The
I am trying to register a type library programatically from VBA code, using two
Part of some vba code I'm writing calls a saved import using: DoCmd.RunSavedImportExport import_name

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.