+ Reply to Thread
Results 1 to 2 of 2

Access 2010 unable to use multiple dates in record in SQL SELECT statement

Hybrid View

Harry73 Access 2010 unable to use... 01-21-2013, 10:47 AM
Harry73 Re: Access 2010 unable to use... 01-24-2013, 10:19 PM
  1. #1
    Registered User
    Join Date
    12-26-2012
    Location
    Indianapolis, IN
    MS-Off Ver
    Excel 2007, 2010
    Posts
    2

    Access 2010 unable to use multiple dates in record in SQL SELECT statement

    I've been pulling my hair out for 2 weeks trying to overcome this issue. I am creating a calendar form in an access 2010 database. I have a set of text boxes that populate the date in the month depending on which month is displayed. That part works good. Underneath each text box, I have a list box to populate last name, first initial and text.

    I am using the code from http://www.tek-tips.com/faqs.cfm?fid=838 to build the text boxes for the calendar. I am reusing the same code for the listboxes, except I changed curday to boxday and I am putting it into a different sub. I can get the correct information to display for 5 of the dates, which are the same type of text. I have 3 other dates which display a different text after the name and I can't get those to work. I know I am going about this the wrong way.

    [FirstDate] is the current month and year in mmmm /yyyy format.

    Here's the code that works:
    Sub StillWorkingItOut()
    
    ' Dim variables
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim boxday As Variant
    Dim i As Integer
    Dim fEmpty As Boolean
    Dim strRowSource As String
    
    ' Assign variables
    Set db = CurrentDb
    Set rs = db.OpenRecordset("qryAppointmentPatient")
    
    ' Date handler
    boxday = DateSerial(Year(Me![FirstDate]), Month(Me![FirstDate]), 1) ' first day of month
    boxday = DateAdd("d", 1 - Weekday(boxday), boxday) ' back to sunday
    ' need to loop thru 42 textboxes
    For i = 0 To 41
    Me("box" & i).ColumnCount = 5
    Me("box" & i).ColumnWidths = ".6 in;.120 in;.195 in;.120 in; .120in"
    fEmpty = (rs.BOF And rs.EOF)
    If fEmpty Then
        strRowSource = Mid(Nz("", "1234567 "), 8)
    Else
        strRowSource = "SELECT qryAppointmentPatient.Lname, qryAppointmentPatient.Fname, " & Chr(34) & "SV" & Chr(34) & " FROM qryAppointmentPatient WHERE (((qryAppointmentPatient.SVA)= #" & Format(boxday, "mm/dd/yyyy") & "#) OR ((qryAppointmentPatient.SVB)= #" & Format(boxday, "mm/dd/yyyy") & "#) OR ((qryAppointmentPatient.SVC)= #" & Format(boxday, "mm/dd/yyyy") & "#) OR ((qryAppointmentPatient.SVD)= #" & Format(boxday, "mm/dd/yyyy") & "#) OR ((qryAppointmentPatient.SVE)= #" & Format(boxday, "mm/dd/yyyy") & "#));"
    End If
    Me("box" & i).RowSource = strRowSource
    Me("box" & i).Visible = False
    If Month(boxday) = Month(Me!FirstDate) Then Me("box" & i).Visible = True
    boxday = boxday + 1 ' nextday
    Next i
    End Sub
    I figured once I could get one of the next dates to populate along with the SVA, SVB, SVC, SVD, and SVE dates, I could add the remaining date fields I need. I know my ColumnCount is wrong, but that doesn't seem to affect how it works. What I am trying to add is another SELECT statement. I've tried using UNION, but then nothing displays. I've learned that UNION statements have to have the same amount of fields so I tried it this way, but nothing displays in the calendar:
    ' adding this to above code (no need to repeat it all)
    Dim strRcrt As String
    strRcrt = " UNION SELECT qryAppointmentPatient.Lname, qryAppointmentPatient.Fname, " & Chr(34) & "R" & Chr(34) & " FROM qryAppointmentPatient WHERE (((qryAppointmentPatient.Recert)= #" & Format(boxday, "mm/dd/yyyy") & "#))"
    strRowSource = "SELECT qryAppointmentPatient.Lname, qryAppointmentPatient.Fname, " & Chr(34) & "SV" & Chr(34) & " FROM qryAppointmentPatient WHERE (((qryAppointmentPatient.SVA)= #" & Format(boxday, "mm/dd/yyyy") & "#))" & strRcrt
    I am guessing that my UNION statement can't have this type of WHERE clause in it. If I remove the WHERE clause, then all the records return with the name and R for every date.

    Let me know if you need any further info to help me out. Thanks!

  2. #2
    Registered User
    Join Date
    12-26-2012
    Location
    Indianapolis, IN
    MS-Off Ver
    Excel 2007, 2010
    Posts
    2

    Re: Access 2010 unable to use multiple dates in record in SQL SELECT statement

    Ok, I finally figured out what I was doing wrong. Of course, it was an error on my part. I was placing the UNION SELECT statement in the wrong section of the code, so it was only being read before the loop, not during the loop. I also realized I could remove all the references to "qryAppointmentPatient" since that was the only query I was pulling records from.

    Here is the working version (for those of you who might benefit from my mistake):

    Sub PopulateCalendar()
    
    ' Dim variables
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim boxday As Variant
    Dim i As Integer
    Dim fEmpty As Boolean
    Dim thatQry As String
    Dim strRowSource As String
    
    ' Assign variables
    Set db = CurrentDb
    Set rs = db.OpenRecordset("qryAppointmentPatient")
    
    ' Date handler
    boxday = DateSerial(Year(Me![FirstDate]), Month(Me![FirstDate]), 1) ' first day of month
    boxday = DateAdd("d", 1 - Weekday(boxday), boxday) ' back to sunday
    ' need to loop thru 42 textboxes
    For i = 0 To 41
    Me("box" & i).ColumnCount = 3
    Me("box" & i).ColumnWidths = ".6 in;.120 in;"
    fEmpty = (rs.BOF And rs.EOF)
    If fEmpty Then
        strRowSource = Mid(Nz("", "1234567 "), 8)
    Else
        thatQry = " UNION SELECT Lname, Fname, " & Chr(34) & "R" & Chr(34) & " FROM qryAppointmentPatient WHERE (((Recert)= #" & Format(boxday, "mm/dd/yyyy") & "#));"
        strRowSource = "SELECT Lname, Fname, " & Chr(34) & "SV" & Chr(34) & " FROM qryAppointmentPatient WHERE (((SVA)= #" & Format(boxday, "mm/dd/yyyy") & "#) OR ((SVB)= #" & Format(boxday, "mm/dd/yyyy") & "#) OR ((SVC)= #" & Format(boxday, "mm/dd/yyyy") & "#) OR ((SVD)= #" & Format(boxday, "mm/dd/yyyy") & "#) OR ((SVE)= #" & Format(boxday, "mm/dd/yyyy") & "#))" & thatQry
    End If
    Me("box" & i).RowSource = strRowSource
    Me("box" & i).Visible = False
    If Month(boxday) = Month(Me!FirstDate) Then Me("box" & i).Visible = True
    boxday = boxday + 1 ' nextday
    Next i
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1