Results 1 to 4 of 4
  1. #1
    Ekhart is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2016
    Posts
    80

    Using SQL with Set Command in VB

    I am trying to take data from an open form and link it to a record on a table using OpenRecordset. Since I have similar code elsewhere I have to assume it has something to do with pulling the data from the form unless I am missing something else Or the fact that this one uses a JOIN.

    Code:
    Set rsConsults = dbs.OpenRecordset("SELECT Scheduler_Sheet.ConsultNumber, Scheduler_Sheet.LastName, Scheduler_Sheet.FirstName, Scheduler_Sheet.SSN, Scheduler_Sheet.Clinic, tblConsultAlias.[Service Name], Scheduler_Sheet.Program FROM Scheduler_Sheet INNER JOIN tblConsultAlias ON Scheduler_Sheet.Clinic = tblConsultAlias.Clinic WHERE (Scheduler_Sheet.[ConsultNumber]) = '" & Forms!frmToChart!ConsultNumber & "'")
    I get an error 'Datatype mismatch in criteria expression'.
    I also tried assigning the data from the form to a variable and got the same error...
    Code:
    Dim FromForm As String
    FromForm = Forms!frmToChart.Form!ConsultNumber
    Set rsConsults = dbs.OpenRecordset("SELECT Scheduler_Sheet.ConsultNumber, Scheduler_Sheet.LastName, Scheduler_Sheet.FirstName, Scheduler_Sheet.SSN, Scheduler_Sheet.Clinic, tblConsultAlias.[Service Name], Scheduler_Sheet.Program FROM Scheduler_Sheet INNER JOIN tblConsultAlias ON Scheduler_Sheet.Clinic = tblConsultAlias.Clinic WHERE (Scheduler_Sheet.[ConsultNumber]) = '" & FromForm & "'")


    I do use the following code elsewhere and it works fine:
    Code:
    Set RSReceiveName = CurrentDb.OpenRecordset("SELECT VHALogin, Fname, Lname, VistaAccess, VistaVerify, SaveVistaLogin FROM tblUpdatedEmployees WHERE (tblUpdatedEmployees.[VHALogin]) = '" & GetUserName & "'")


  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If ConsultNumber has a numeric data type, you don't want the apostrophes around the value.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Code:
    WHERE (Scheduler_Sheet.[ConsultNumber]) = '" & Forms!frmToChart!ConsultNumber & "'")
    If ConsultNumber actually is defined as a number then it can't have the quotes around it:
    Code:
    WHERE Scheduler_Sheet.[ConsultNumber] = " & Forms!frmToChart!ConsultNumber)
    Last edited by davegri; 12-22-2017 at 09:59 AM. Reason: syntax

  4. #4
    Ekhart is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2016
    Posts
    80
    That got it, thanks! Something so simple yet easy to miss.

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

Similar Threads

  1. Command button help
    By anastam in forum Access
    Replies: 3
    Last Post: 03-21-2016, 09:22 AM
  2. Replies: 3
    Last Post: 03-29-2015, 07:42 PM
  3. Replies: 1
    Last Post: 09-12-2014, 06:09 AM
  4. Replies: 3
    Last Post: 08-04-2013, 07:11 AM
  5. Replies: 1
    Last Post: 07-27-2010, 02:27 PM

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