Results 1 to 6 of 6
  1. #1
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74

    Run-time error 3075 with DLookup when row value contains an apostrophe

    Title says it all. Anytime there is an apostrophe in the cell it throws error 3075.

    Code:
    Me.tboxScientific = DLookup("[ScientificName]", "Species", "[CommonName] = '" & Me.cboCommonName.Text & "'")
    Should I include logic to add an escape character when the code encounters an apostrophe? I'm new to VBA, but I understand the concept in other languages.

    I tried changing the code to the following (wrapped the final expression in quotes instead of apostrophes [' " '] instead of [" ' "], which works in Python but not in this VBA case I guess -- syntax error):

    Code:
    Me.tboxScientific = DLookup("[ScientificName]", "Species", '[CommonName] = "' & Me.cboCommonName.Text & '"')


  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I would do this a little differently.
    It looks like you have some table named "Species" that list the Common Name and Scientific Name fields.
    On your Form, it looks like you have a Combo Box named "cboCommonName". I am guessing that the values from this Combo Box come from your Species table.
    If that is the case, there is no need to use DLOOKUP. Simply set up your Combo Box to show both fields (Common Name and Scientific Name), and then use the AfterUpdate Event on the Combo Box to populate tboxScientific with the value from the second column of your Combo Box selection, i.e.
    Code:
    Private Sub cboCommonName_AfterUpdate()
        Me.tboxScientific = Me.cboCommonName.Column(1)
    End Sub

    Edit: The first column in the Combo Box is given an oridinal of 0, then next 1, etc.

  3. #3
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74
    This is excellent.

    All of my database experience is in GIS and spatial databases, not in access. I've been trying to hack together a solution to synchronize two separate combo boxes, which is where the text boxes came from in the first place.

    How do I get the combo box to show both fields? Is it a concatenation in the combobox's RowSource SQL?

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If you use the Combo Box Wizard, you can select both fields when going through the Wizard.
    Or, if updating it manually, update the SQL code in the "Row Source" property. Just be sure to also update the "Column Count" property to 2.

  5. #5
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74
    Thanks, this is exactly what I was looking for.

    In order to access the values i just use the .Column() method?

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Yes, and the Columns are Zero-based, meaning the first Column is 0, the second Column is 1, etc.

    I agree with Joe's idea about the Combobox, but FYI, to handle the Apostrophe in the DLookup Function (and elsewhere) you'd use something like this, using extra Quotation Marks:

    Code:
    Me.tboxScientific = DLookup("[ScientificName]", "Species", '[CommonName] = """ & Me.cboCommonName & """")
    Notice that I also dropped the .Text from your code. There's no need for it, because the Default Property, which is .Value, will work, and being the Default Property, doesn't even need to be written out.

    Linq ;0)>

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Need Help with run time error 3075
    By vkumar in forum Forms
    Replies: 14
    Last Post: 07-24-2012, 05:48 PM
  2. Run Time Error '3075'
    By ertweety in forum Programming
    Replies: 2
    Last Post: 06-03-2012, 04:26 PM
  3. Replies: 6
    Last Post: 05-30-2012, 12:32 PM
  4. Replies: 5
    Last Post: 03-27-2012, 01:40 PM
  5. Run Time Error 3075 in Access 2007
    By jblank65 in forum Programming
    Replies: 6
    Last Post: 01-25-2011, 04:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums