Results 1 to 19 of 19

Thread: [RESOLVED] Mirroring Checkbox states

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Location
    Gorgeous Ontario
    Posts
    265

    Resolved [RESOLVED] Mirroring Checkbox states

    I have a VB application that has a userform to fill. The data being entered is submitted to an Access dB. I have a print button to print the data, however, because of reasons too long to explain, I have to print from my form template in excel.

    I have checkboxes on my VB form and checkboxes on the excel worksheet. How do I go about getting the excel checkboxes to mirror the states of the VB app when I engage the load & print subroutine?

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Mirroring Checkbox states

    Do the matched pairs of checkboxes have the same name in the excel ssheet and in the VB app?
    If so, you could pass the name and value of each VB chkbox into a 2D array and then loop through the Excel chkboxes, setting the value to that stored in the array.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Location
    Gorgeous Ontario
    Posts
    265

    Re: Mirroring Checkbox states

    Here is what I'm attempting, with no success. Everything works but the checkboxes.
    VB Code:
    1. If SSTab1.Tab = 1 Then
    2.         Set oXLApp = New Excel.Application  'Create a new instance of Excel
    3.         Set oXLBook = oXLApp.Workbooks.Open(App.Path & "\TRW_Template.xls") 'Open Daily Data Template
    4.         oXLApp.Visible = False              'Hide Excel Spreadsheet
    5. '************************************************************************************************
    6.         Worksheets("Safety Flash Report").Range("A3").Value = Date
    7. '************************************************************************************************
    8.         For DataIndex = 0 To 11
    9.             If chkFlash(DataIndex).Value = vbChecked Then
    10.                 Worksheets("Safety Flash Report").CheckBox(DataIndex + 1).Value = vbChecked    'checkboxes
    11.             End If
    12.         Next
    13. '************************************************************************************************
    14.         FlashDateRaw = CDate(cboFlash(0).Text & "/" & cboFlash(1).Text & "/" & cboFlash(2).Text)
    15.  etc....

    I'm not sure what's missing but I've stated the appropriate sheet the checkboxes...what do I change on the Excel chkboxes? This is my first time with the VBA implementation of VB.

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Mirroring Checkbox states

    VBA dosn't support Control Array as VB does. Therefore, I assume the problem you are haveing relates to the following line:

    Worksheets("Safety Flash Report").CheckBox(DataIndex + 1).Value = vbChecked


    Right?

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Mirroring Checkbox states

    Is placing the CheckBox's on a UserForm an option?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Location
    Gorgeous Ontario
    Posts
    265

    Re: Mirroring Checkbox states

    No I can't use a VBA Userform. The worksheet is a report print-out, I am simply filling out the worksheet with my program data so I have a consistent looking report.

    So how do I go about getting this to work? Remember I have very little VBA experience.

    Here is what I am trying to do, file attached, second worksheet.
    Attached Files Attached Files

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mirroring Checkbox states

    vbchecked doesn't seem valid, try true or false

    CheckBox An integer value indicating whether the item is selected:
    Null Indicates the item is in a null state, neither selected nor cleared.
    –1 True. Indicates the item is selected.
    0 False. Indicates the item is cleared.

    Copyright(c) 1996 Microsoft Corporation.
    vbchecked returns null

    pete

  8. #8
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    485

    Re: Mirroring Checkbox states

    westconn1 nailed it!

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Mirroring Checkbox states

    Quote Originally Posted by Harddisk
    westconn1 nailed it!

    That would be true if he was exclusively using VBA. However, he is using VB (as stated in post #1) and loading an instance of Excel (from within VB).
    Therefore - vbChecked is valid.

    The issue is there are no Control Arrays in VBA, therefore you cannot pass thru them with an index etc.

  10. #10
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    485

    Re: Mirroring Checkbox states

    But you see, what determines the value that the property Value (pardon the pun) is on the control itself, despite the programming languages you use.

    From what I see, in Excel, the controls are probably based on Form 2.0, which is an older version of Form Controls if you compared them to the controls on VB6 (can't remember what Form version they are using, probably 2.1?) itself.

    Hence, by assigning True/False directly towards the control via the Excel object, it might just work.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mirroring Checkbox states

    But you see, what determines the value that the property Value (pardon the pun) is on the control itself, despite the programming languages you use.
    regardless of whether vbchecked has the right value (as it has in this case, but not in vba)
    you can not check the checkbox without addressing it individually
    sheet1.checkbox1.value = true (or vbchecked)
    sheet1.checkbox2.value = true (or vbchecked)
    sheet1.checkbox3.value = true (or vbchecked)
    sheet1.checkbox4.value = true (or vbchecked)
    sheet1.checkbox5.value = true (or vbchecked)
    sheet1.checkbox6.value = true (or vbchecked)
    etc...etc.

    pete

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mirroring Checkbox states

    The issue is there are no Control Arrays in VBA, therefore you cannot pass thru them with an index etc.
    ok... you can loop through the checkboxs on a sheet, they are part of the oleobjects collection and can be checked or unchecked as part of a for loop code
    VB Code:
    1. With Sheet4
    2. For i = 1 To .OLEObjects.Count
    3. If Not InStr(.OLEObjects(i).Name, Check) = 0 Then
    4. .OLEObjects(i).Object.Value = True
    5. End If
    6. Next
    7. End With
    , or for the specific code in this post
    VB Code:
    1. For dataindex = 0 To 11
    2.             If chkFlash(dataindex).Value = vbchecked Then
    3.                 Worksheets("Safety Flash Report").OLEObjects(dataindex).Object.Value = vbchecked     'checkboxes
    4.             End If      ' if there are other controls need to check that they are the checkboxes
    5.         Next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Location
    Gorgeous Ontario
    Posts
    265

    Re: Mirroring Checkbox states

    I understand where you are going with this, but what do I have to configure on the Excel template work sheet?

    So I have a number of checkboxes named Checkbox1,Checkbox2.....
    They each have the same formula "=EMBED("Forms.CheckBox.1","")"

    And I have this:
    VB Code:
    1. For DataIndex = 0 To 11
    2.             If chkFlash(DataIndex).Value = vbChecked Then
    3.                 Worksheets("Safety Flash Report").OLEObjects(DataIndex).Object.Value = vbChecked
    4.             End If
    5.         Next

    What am I missing?

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Location
    Gorgeous Ontario
    Posts
    265

    Re: Mirroring Checkbox states

    Now this works:
    VB Code:
    1. If chkFlash(DataIndex).Value = vbChecked Then
    2.                 Worksheets("Safety Flash Report").CheckBox1.Value = vbChecked
    3.             End If

    Is there a way to select my index, add 1 to it and add it to the end of "Checkbox"?

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mirroring Checkbox states

    do you have other ole objects on you worksheet other than the checkboxes?
    For DataIndex = 0 To 11
    If chkFlash(DataIndex).Value = vbChecked Then
    Worksheets("Safety Flash Report").OLEObjects(DataIndex).Object.Value = vbChecked
    End If
    Next
    basically relies on there being no other olebjects except for the checkboxes
    otherwise you need to extract the right part of the checkbox name ie.the number and use that as the index

    i think this will work, it is using the name of the checkbox to represent the index from your vb

    VB Code:
    1. With Worksheets("Safety Flash Report")
    2. For i = 1 To .OLEObjects.Count
    3. If Not InStr(.OLEObjects(i).Name, Check) = 0 Then
    4.     myindex = Right(.OLEObjects(i).Name, Len(.OLEObjects(i).Name) - Len("Checkbox"))
    5.     If chkFlash(myindex).value = vbChecked Then
    6.                 .OLEObjects(i).object.value = vbChecked
    7.             Else: .OLEObjects(i).object.value = False
    8.             End If
    9.                       ' or .oleobjcts(i).object.value = chkflash(myindex).value
    10. End If
    11. Next
    12. End With
    so CheckBox11 mirrors chkFlash(11)
    if you have a chkFlash(0), you will need to put myindex -1 when testing the value of chkFlash, then CheckBox11 will mirror chkFlash(10)


    Edit: fix error
    Last edited by westconn1; Sep 1st, 2006 at 05:21 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Location
    Gorgeous Ontario
    Posts
    265

    Re: Mirroring Checkbox states

    Lets try just one!
    VB Code:
    1. If chkFlash(1).Value = vbChecked Then
    2.             Worksheets("Safety Flash Report").OLEObjects("CheckBox1").object.Value = vbChecked
    3.         End If
    What am I doing wrong here?

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mirroring Checkbox states

    it should work, as close as i can test it does
    at this point, what is the value of vbchecked?
    it has to be true, or false, if it is empty it will give grayed check

    what does it do? is there an error?

    do you want to post your project
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mirroring Checkbox states

    i don't know why the methods are not exposed to vb program, but i have tested this from a vb6 project, with some checkboxes. it works and the correct value is saved, but sometimes the checkboxes show checked when they should be unchecked or vise versa even though it's value is correct.

    sorry if this is confusing, on reopening the workbook the checkboxes are correct

    here is the code i used
    VB Code:
    1. Dim xlapp As Object
    2. Dim mybook As Object
    3. Dim myindex
    4. On Error GoTo MyError
    5.     Set xlapp = GetObject(, "Excel.Application")
    6.     If TypeName(xlapp) = "Nothing" Then
    7.         Set xlapp = CreateObject("Excel.Application")
    8.     End If
    9.       Set mybook = xlapp.workbooks.Open("C:\Documents and Settings\peter\Application Data\Microsoft\Excel\XLSTART\test.xls")
    10.     xlapp.Visible = True
    11.    
    12. With mybook.sheets("Data")
    13. For i = 1 To .Shapes.Count
    14. If Not InStr(.Shapes(i).Name, "Check") = 0 Then
    15.     myindex = Right(.Shapes(i).Name, Len(.Shapes(i).Name) - Len("Checkbox"))
    16.         .Shapes(i).OLEFormat.object.object.Value = Check1(myindex - 1).Value
    17. End If
    18. Next
    19.  
    20. End With
    21.    
    22. mybook.Save
    23. mybook.Close
    24. xlapp.Quit False
    25.  
    26. Set xlapp = Nothing
    Last edited by westconn1; Sep 2nd, 2006 at 04:00 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Location
    Gorgeous Ontario
    Posts
    265

    Re: Mirroring Checkbox states

    I actually got it working 5 min before leaving for the long weekend on friday. I put up a post but I guess it didn't go through. Here is how I solved it using your help:
    VB Code:
    1. If SSTab1.Tab = 1 Then
    2.         Set oXLApp = New Excel.Application  'Create a new instance of Excel
    3.         Set oXLBook = oXLApp.Workbooks.Open(App.Path & "\TRW_Template.xls") 'Open Daily Data Template
    4.         Set oXLSheet = oXLBook.Worksheets("Safety Flash Report")
    5.         oXLApp.Visible = False              'Hide Excel Spreadsheet
    6.     '********************************************************************************************
    7.         oXLSheet.Range("A3").Value = Date
    8.     '********************************************************************************************
    9. Dim Ctrl As Object
    10.  
    11.         For Each Ctrl In oXLSheet.OLEObjects
    12.             If Ctrl.OLEType = 2 Then
    13.                 For DataIndex = 1 To 12
    14.                     If Replace(Ctrl.Name, "CheckBox", "") = DataIndex Then
    15.                         If chkFlash(DataIndex).Value = vbChecked Then
    16.                             If Ctrl.object.Value = False Then Ctrl.object.Value = True
    17.                         End If
    18.                         Exit For
    19.                     End If
    20.                 Next
    21.             End If
    22.         Next
    23.     '********************************************************************************************
    I used the code from above, and I also added "Set oXLSheet = oXLBook.Worksheets("Safety Flash Report")" because I had been getting a number of runtime errors from the Worksheet object not being 100% defined.

    Thanks for the help West.

Posting Permissions

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



Click Here to Expand Forum to Full Width