Page 1 of 4 123 ... LastLast
Results 1 to 15 of 49
  1. #1
    Join Date
    Nov 2009
    Location
    UK
    Posts
    44

    Unanswered: Using more than 7 IF statements (or simplifying)

    Hi,

    I am struggling with setting a number of cmd buttons to enable after a field has been updated.

    What i am looking to do is enable a button if a field is either Tues,Wed,Thurs Fri,Sat but disable if Sun,Mon.

    I have the if statement set up and it works great but i have 2 buttons for each date (like a calendar) AM and PM so i have 2 if statements for each date and need to do the same for 14 dates (28 if statements)

    I am new to access and would appreciate any help. The statement is:
    If Me.dayname1 = "Tuesday" Then
    Me.cmdday1am.Enabled = True
    ElseIf Me.dayname1 = "Wednesday" Then
    Me.cmdday1am.Enabled = True
    ElseIf Me.dayname1 = "Thursday" Then
    Me.cmdday1am.Enabled = True
    ElseIf Me.dayname1 = "Friday" Then
    Me.cmdday1am.Enabled = True
    ElseIf Me.dayname1 = "Saturday" Then
    Me.cmdday1am.Enabled = True
    Else: Me.cmdday1am.Enabled = False

    End If

    Hope you understand and can help me out

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    I'm not clear on the rest of it, but this can easily be simplified to:

    Code:
    If Me.dayname1 = "Sunday" OR Me.dayname1 = "Monday" Then
      Me.cmdday1am.Enabled = False
    Else
      Me.cmdday1am.Enabled = True
    End If
    If you're saying Me.dayname1 repeats like Me.dayname2, then I'd use a For/Next loop along with that.
    Paul

  3. #3
    Join Date
    Nov 2009
    Location
    UK
    Posts
    44
    Thanks,

    I can understand the simplicity but i will still have trouble creating the rest of the statements.

    This is going to be a booking form
    Each day has 2 buttons (AM/PM) and i would like to display 14 days like a calendar, when a button is pressed to populate the calendar i would like each day to either display the AM/PM of disable the AM/PM buttons so that for example on a Sunday the user cannot press the AM or PM buttons.

    14 days - 28 Buttons (maybe i have over complicated things but that's what they want to see at work) Not sure what you mean by the For/Next loop.

  4. #4
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    So, are you saying the control names repeat like that?
    Paul

  5. #5
    Join Date
    Nov 2009
    Location
    UK
    Posts
    44
    yep, each field and button is incremented by 1
    I have attached a screen shot if that helps
    Attached Thumbnails Attached Thumbnails Screenshot.bmp  

  6. #6
    Join Date
    Nov 2009
    Location
    UK
    Posts
    44
    as you can see there are 14 days, these are calculated fields so are not static, when the user presses the find next available button the "calendar" starts from the next available appointment date. The field "Monday","Tuesday" etc is a formatted field based on the date below it. I can get it working but only up to day 7, then i think i must run out of IF statements because the buttons dont update like they should.

  7. #7
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    The guts of it:

    Code:
    For x = 1 to 14
      If Me("dayname" & x) = "Sunday" OR Me("dayname" & x)  = "Monday" Then
        Me("cmdday" & x & "am").Enabled = False
    Paul

  8. #8
    Join Date
    Nov 2009
    Location
    UK
    Posts
    44
    ps the code is placed on the on click action of the find next available and calendar buttons.

    Thanks for your help Paul

  9. #9
    Join Date
    Nov 2009
    Location
    UK
    Posts
    44
    Thanks, i have tried the code but comes up with compile error "for without next" i know its probably easy but not sure what the problem is

  10. #10
    Join Date
    Nov 2009
    Location
    UK
    Posts
    44
    Paul you are a superstar, i have added the else and sorted out the compile error and its working like a charm.

    Just a quick one i added Next just before the end of the code, what does the next do? (am i correct in thinking it cycles through the code until all button have been repainted?)

    Cant believe it was so simple (for an expert)

  11. #11
    Join Date
    Nov 2009
    Location
    UK
    Posts
    44
    now that i have this statement working i wonder if you could help me a little further. I would like to run the statement again but this time compare the actual date field [day1] etc. to a table of unavailable days (holidays etc.) and perform the same operation. So if for example a Tuesday which would normally enable the buttons was stored in the table "date" this would see if the date matched and if it did it would disable the buttons like above. Not sure if this is possible

  12. #12
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Glad you sorted it out. What I posted was not intended to be complete, but rather to guide you towards a solution. The Next advances x to the next value, so in the example code, whatever is between For and Next will be executed 14 times.

    You could add a test using DCount() to test the date against the table:

    If Me("dayname" & x) = "Sunday" OR Me("dayname" & x) = "Monday" OR DCount(...) > 0 Then

    More info on the syntax for the DCount() here:

    General: DLookup Usage Samples
    Paul

  13. #13
    Join Date
    May 2005
    Posts
    1,191
    Quote Originally Posted by leebaggaley View Post
    Just a quick one i added Next just before the end of the code, what does the next do? (am i correct in thinking it cycles through the code until all button have been repainted?)
    The For Loop is one of those control loops that is fundamental to any programming language. Just about every programming language has something like it (any language worth it's salt at least ). The VB For Loop simply requires the "Next" at the end to tell the code what it is looping over, and when it's time to go to the next variable in the loop.

    Quote Originally Posted by leebaggaley View Post
    I would like to run the statement again but this time compare the actual date field [day1] etc. to a table of unavailable days (holidays etc.) and perform the same operation. So if for example a Tuesday which would normally enable the buttons was stored in the table "date" this would see if the date matched and if it did it would disable the buttons like above. Not sure if this is possible
    That's a little more complex. The best way I can think of to do it would be to open a recordset based on the table, and then do a nested loop (since you have to loop over each control, and each control needs to loop against the table to check). I don't know how good your programming skills are, but give that a shot and let us know if/what you need help. Cheers!
    Me.Geek = True

  14. #14
    Join Date
    May 2005
    Posts
    1,191
    (Double post)
    Last edited by nckdryr; 11-03-09 at 00:16. Reason: Double Post
    Me.Geek = True

  15. #15
    Join Date
    Nov 2009
    Location
    UK
    Posts
    44
    Thanks for your help guys i will look into your suggestions today

Posting Permissions

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