Results 1 to 4 of 4
  1. #1
    T_Tronix is offline Novice
    Windows 8 Access 2007
    Join Date
    Nov 2013
    Posts
    28

    Run Query in VB code

    I have a forum where I am using a checkbox. Now I only want one record to have this checkbox = true so I created a query that checked if there are any records with this checkbox as "true". But now I want to execute this query once someone clicks on the checkbox field in a new record (to give an error message if the query is not null that the checkbox was already checked in another record).



    I got this code but keep getting errors:

    Dim dbsCurrent As Database
    Dim rsCount As Recordset
    Dim result As String

    Set dbsCurrent = CurrentDb
    Set rsCount = dbsCurrent.OpenRecordset("SELECT Tasks.Resource_F, Tasks.Resource_L, Tasks.Project_Lead, Tasks.Resource_ACF2ID FROM Tasks WHERE(((Tasks.Project_Name)=[forms]![frmTasks]![cmbProject]) AND ((Tasks.Project_Lead)=True))")

    result = rsCount![Resource_F]
    result1 = rsCount![Resource_L]
    rsCount.Close


    MsgBox "There is already someone checked:" & result & " " & result1, vbInformation, "Example"
    Me!chkLead = False
    Exit Sub
    End If

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,643
    Variables cannot be enclosed in quote marks, they must be concatenated. Reference to control on form is a variable.

    Set rsCount = dbsCurrent.OpenRecordset("SELECT Resource_F, Resource_L, Project_Lead, Resource_ACF2ID FROM Tasks WHERE Tasks.Project_Lead=True AND Project_Name='" & [forms]![frmTasks]![cmbProject]) & "';")

    Assumes Project_Name is a text data type.

    Alternative to opening a recordset is DCount(). Access Help has info about domain aggregate functions.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You have to concatenate the value of the combo box to the string, otherwise you are looking for the string "[Forms]![frmTasks]![cmbProject]"

    This might get you moving again:
    (Assumes the combo box is text)
    Code:
       Dim dbsCurrent As DAO.Database
       Dim rsCount As DAO.Recordset
       Dim result As String
       Dim sSQL As String
    
       Set dbsCurrent = CurrentDb
    
       sSQL = "SELECT Tasks.Resource_F, Tasks.Resource_L, Tasks.Project_Lead, Tasks.Resource_ACF2ID"
       sSQL = sSQL & " FROM Tasks"
       sSQL = sSQL & " WHERE Tasks.Project_Name = '" & [Forms]![frmTasks]![cmbProject] & "' AND Tasks.Project_Lead =True"
    
       Set rsCount = dbsCurrent.OpenRecordset(sSQL)
    
       result = rsCount![Resource_F]
       '   result1 = rsCount![Resource_L]
       
       rsCount.Close
       Set rsCount = Nothing
       Set dbsCurrent = Nothing
    
       If result > 0 Then
          MsgBox "There is already someone checked:" & result & " " & result1, vbInformation, "Example"
          Me!chkLead = False
       End If
    edit: I see June beat me again

  4. #4
    T_Tronix is offline Novice
    Windows 8 Access 2007
    Join Date
    Nov 2013
    Posts
    28
    fixed it up:

    Code:
    strSQL = "SELECT Tasks.Resource_F, Tasks.Resource_L, Tasks.Project_Lead, Tasks.Resource_ACF2ID"
    strSQL = strSQL & " FROM Tasks"
    strSQL = strSQL & " WHERE Tasks.Project_Name = " & "'" & Me!cmbProject & "'" & " AND Tasks.Project_Lead= True;"
    Works now...

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

Similar Threads

  1. Replies: 1
    Last Post: 05-04-2013, 12:19 PM
  2. Update Query by VBA code
    By ice051505 in forum Programming
    Replies: 10
    Last Post: 02-25-2013, 10:49 AM
  3. Query using VBA code
    By pepok4 in forum Programming
    Replies: 13
    Last Post: 02-05-2013, 02:17 PM
  4. query using code
    By rohini in forum Queries
    Replies: 1
    Last Post: 05-17-2012, 03:46 AM
  5. VBA code to display value from SQL query
    By agent- in forum Programming
    Replies: 9
    Last Post: 05-05-2011, 03:52 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