Results 1 to 6 of 6

Thread: VB MENU CODING

  1. #1
    Star Lounger
    Join Date
    Apr 2001
    Location
    SOUTH AFRICA
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VB MENU CODING

    Hi there,
    I have just started SAMS 21 DAY vb training and am battling with a small project. I cannot get the menu underline, bold and italic to work properly. Here is my coding to add a check to the menu and change the label caption.

    Private Sub chkBold_Click()
    If chkBold.Value = Checked Then
    txtText1.FontBold = True
    lblProject1.FontBold = True
    mnuFontBold.Checked = True
    Else
    chkBold.Value = Unchecked
    txtText1.FontBold = False
    lblProject1.FontBold = False
    mnuFontBold.Checked = False
    End If
    End Sub

    Private Sub chkItalic_Click()
    If chkItalic.Value = Checked Then
    txtText1.FontItalic = True
    lblProject1.FontItalic = True
    mnuFontItalic.Checked = True
    Else
    chkItalic.Value = Unchecked
    txtText1.FontItalic = False
    lblProject1.FontItalic = False
    mnuFontItalic.Checked = False
    End If
    End Sub

    Private Sub chkUnderline_Click()
    If chkUnderline.Value = Checked Then
    txtText1.FontUnderline = True
    lblProject1.FontUnderline = True
    mnuFontUnderline.Checked = True
    Else
    chkUnderline.Value = Unchecked
    txtText1.FontUnderline = False
    lblProject1.FontUnderline = False
    mnuFontUnderline.Checked = False
    End If
    End Sub
    Thanks a lot

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: VB MENU CODING

    Hi Rene,

    The main reason you are having trouble is because you have not assigned a value to 'Checked', and it therefore evaluates to false. Either assign as follows :

    Dim Checked as Boolean
    Checked = True

    or just use the following :

    If chkBold.Value = True Then

    if If chkBold.Value is not True, then it must be False, so you do not have a need for chkBold.Value = Unchecked, and so can omit that line.

    So your completed procedure might look like :

    Private Sub chkBold_Click()
    If chkBold.Value = True Then
    txtText1.FontBold = True
    lblProject1.FontBold = True
    mnuFontBold.Checked = True
    Else
    txtText1.FontBold = False
    lblProject1.FontBold = False
    mnuFontBold.Checked = False
    End If
    End Sub

    The same applies for the rest of your procedures.

    Good luck with your training.

    Andrew C

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VB MENU CODING

    Actually, there's a simpler way. In VB, a checked checkbox will return a 1 (not the minus 1 it returns in VBA) or a 0 unless it's tri-state. Since any not-zero value evaluates to true, and assuming that there is no earthly use for a tri-state checkbox in this situation, you could write your code more simply like this:

    txtText1.FontBold = chkBold.Value
    lblProject1.FontBold = chkBold.Value
    mnuFontBold.Checked = chkBold.Value

    That way, you don't have to use a conditional structure at all. If chkBold is true, the textbox and label are bolded and the menu item is checked. Otherwise, they aren't.
    Charlotte

  4. #4
    Star Lounger
    Join Date
    Apr 2001
    Location
    SOUTH AFRICA
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB MENU CODING

    Hi Andrew & Charlotte,
    Thank you very much for the info. Can you please give me advise where I can get the common VBA coding from, as I work a lot with Excel, and the VB is not actullay helping me with what I do.
    Andrew helped me a lot with VBA coding, but I am still battling with ie: creating a pivot table with data that is filtered by auto filter. I get all the data in the pivot table instead of only the filtered data rows.

    Thanks one again.

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: VB MENU CODING

    Hi Rene,

    Charlotte's code is the more efficient and I hope you follow it. What I posted was just an indication of where your own code had gone wrong.

    I noticed your post re Pivot Table problem in the Excel Foprum, but you seem to have omitted to attach the file you mentioned. Perhaps you could do so as I think it would be helpful in sorting the problem. It is probably best to leave the problem in the Excel forum.

    Andrew

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VB MENU CODING

    I agree that the Excel forum is the best place to get your coding answers. Also look for a book on Excel that covers this kind of stuff. If you generally work in VBA for a particular application, a VB book will probably confuse you more than it will help you.
    Charlotte

Posting Permissions

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