Results 1 to 11 of 11

Thread: [RESOLVED] There's got to be a better way...

  1. #1

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Resolved [RESOLVED] There's got to be a better way...

    Hi,

    I am currently generating a list inside a Word 2003 document based on choices made in a userform. Each item has a price so I have set up strings for each one:

    VB Code:
    1. Dim strProtectionCoversPrice As String, strChainShortenersPrice, strDetachTopBarPrice As String, _
    2.         strFireExtingExtPrice As String, strFireExtingIntPrice As String, strFirstAidKitPrice As String, strFlipUpBarPrice As String, _
    3.         strHazChemPrice As String, strHighTippingPrice As String, strHoldToRunPrice As String, strInCabCtrlsPrice As String, _
    4.         strLoadCellHangerPrice As String, strLoadCellSubEquipPrice As String, strAddPrinterPrice As String, _
    5.         strNetBoxBasic620Price As String, strNetBoxBasic880Price As String, strNetBoxDropDownPrice As String, _
    6.         strPressureFilterPrice As String, strRearLightProtectionPrice As String, strRevBuzzerPrice As String, _
    7.         strRevCameraPrice As String, strBeaconPrice As String, strHyTowerPrice As String, strWingSPRearBogiePrice As String, _
    8.         strWingSRearBogiePrice As String, strWingPRearAxlePrice As String, strWingPRearBogiePrice As String, _
    9.         strWingPRearSteerPrice As String, strToolboxPrice As String, strWinchPrice As String, _
    10.         strWorkLightSinglePrice As String, strWorkLightTwinPrice As String, strFinishPaintEquipPrice As String
    11.  
    12.     strProtectionCoversPrice = "100.00"
    13.     strChainShortenersPrice = "200.00"
    14.     strDetachTopBarPrice = "300.00"
    15.     strFireExtingExtPrice = "400.00"
    16.     strFireExtingIntPrice = "500.00"
    17.     strFirstAidKitPrice = "600.00"
    18.     strFlipUpBarPrice = "700.00"
    19.     strHazChemPrice = "800.00"
    20.     strHighTippingPrice = "900.00"
    21.     strHoldToRunPrice = "50.00"
    22.     strInCabCtrlsPrice = "1000.00"
    23.     strLoadCellHangerPrice = "2000.00"
    24.     strLoadCellSubEquipPrice = "3000.00"
    25.     strAddPrinterPrice = "300.00"
    26.     strNetBoxBasic620Price = "100.00"
    27.     strNetBoxBasic880Price = "110.00"
    28.     strNetBoxDropDownPrice = "175.00"
    29.     strPressureFilterPrice = "150.00"
    30.     strRearLightProtectionPrice = "100.00"
    31.     strRevBuzzerPrice = "50.00"
    32.     strRevCameraPrice = "350.00"
    33.     strBeaconPrice = "50.00"
    34.     strHyTowerPrice = "1500.00"
    35.     strWingSPRearBogiePrice = "250.00"
    36.     strWingSRearBogiePrice = "350.00"
    37.     strWingPRearAxlePrice = "100.00"
    38.     strWingPRearBogiePrice = "200.00"
    39.     strWingPRearSteerPrice = "125.00"
    40.     strToolboxPrice = "95.00"
    41.     strWinchPrice = "500.00"
    42.     strWorkLightSinglePrice = "50.00"
    43.     strWorkLightTwinPrice = "75.00"
    44.     strFinishPaintEquipPrice = "900.00"

    The contents of the list will vary depending on choices made in a userform. If the items checkbox is not selected, but is enabled, then the item will go in the list:

    VB Code:
    1. Dim strProtectionCovers As String, strChainShorteners, strDetachTopBar As String, _
    2.         strFireExtingExt As String, strFireExtingInt As String, strFirstAidKit As String, strFlipUpBar As String, _
    3.         strHazChem As String, strHighTipping As String, strHoldToRun As String, strInCabCtrls As String, _
    4.         strLoadCellHanger As String, strLoadCellSubEquip As String, strAddPrinter As String, _
    5.         strNetBoxBasic620 As String, strNetBoxBasic880 As String, strNetBoxDropDown As String, _
    6.         strPressureFilter As String, strRearLightProtection As String, strRevBuzzer As String, _
    7.         strRevCamera As String, strBeacon As String, strHyTower As String, strWingSPRearBogie As String, _
    8.         strWingSRearBogie As String, strWingPRearAxle As String, strWingPRearBogie As String, _
    9.         strWingPRearSteer As String, strToolbox As String, strWinch As String, strWorkLightSingle As String, _
    10.         strWorkLightTwin As String, strFinishPaintEquip As String, strAvailableOptions As String
    11.        
    12.     If chkAddProtectionCovers.Enabled = True And chkAddProtectionCovers.Value = False Then
    13.         strProtectionCovers = vbCr & chkAddProtectionCovers.Caption & vbTab & "£" & vbTab & strProtectionCoversPrice
    14.     End If
    15.    
    16.     If chkAddChainShorteners.Enabled = True And chkAddChainShorteners.Value = False Then
    17.         strChainShorteners = vbCr & chkAddChainShorteners.Caption & vbTab & "£" & vbTab & strChainShortenersPrice
    18.     End If
    19.    
    20.     If chkAddDetachTopBar.Enabled = True And chkAddDetachTopBar.Value = False Then
    21.         strDetachTopBar = vbCr & chkAddDetachTopBar.Caption & vbTab & "£" & vbTab & strDetachTopBarPrice
    22.     End If
    23.    
    24.     If chkAddFireExtingExt.Enabled = True And chkAddFireExtingExt.Value = False Then
    25.         strFireExtingExt = vbCr & chkAddFireExtingExt.Caption & vbTab & "£" & vbTab & strFireExtingExtPrice
    26.     End If
    27.    
    28.     If chkAddFireExtingInt.Enabled = True And chkAddFireExtingInt.Value = False Then
    29.         strFireExtingInt = vbCr & chkAddFireExtingInt.Caption & vbTab & "£" & vbTab & strFireExtingIntPrice
    30.     End If
    31.    
    32.     If chkAddFirstAidKit.Enabled = True And chkAddFirstAidKit.Value = False Then
    33.         strFirstAidKit = vbCr & chkAddFirstAidKit.Caption & vbTab & "£" & vbTab & strFirstAidKitPrice
    34.     End If
    35.    
    36.     If chkAddFlipUpBar.Enabled = True And chkAddFlipUpBar.Value = False Then
    37.         strFlipUpBar = vbCr & chkAddFlipUpBar.Caption & vbTab & "£" & vbTab & strFlipUpBarPrice
    38.     End If
    39.    
    40.     If chkAddHazChem.Enabled = True And chkAddHazChem.Value = False Then
    41.         strHazChem = vbCr & chkAddHazChem.Caption & vbTab & "£" & vbTab & strHazChemPrice
    42.     End If
    43.    
    44.     If chkAddHighTipping.Enabled = True And chkAddHighTipping.Value = False Then
    45.         strHighTipping = vbCr & chkAddHighTipping.Caption & vbTab & "£" & vbTab & strHighTippingPrice
    46.     End If
    47.    
    48.     If chkAddHoldToRun.Enabled = True And chkAddHoldToRun.Value = False Then
    49.         strHoldToRun = vbCr & chkAddHoldToRun.Caption & vbTab & "£" & vbTab & strHoldToRunPrice
    50.     End If
    51.    
    52.     If chkAddInCabCtrls.Enabled = True And chkAddInCabCtrls.Value = False Then
    53.         strInCabCtrls = vbCr & chkAddInCabCtrls.Caption & vbTab & "£" & vbTab & strInCabCtrlsPrice
    54.     End If
    55. and so on.....

    The list is inserted at a bookmark using another bookmark, that is all the choices, although any null strings will be ignored.

    VB Code:
    1. strAvailableOptions = strProtectionCovers & strChainShorteners & strDetachTopBar & _
    2.         strFireExtingExt & strFireExtingInt & strFirstAidKit & strFlipUpBar & strHazChem & _
    3.         strHighTipping & strHoldToRun & strInCabCtrls & strLoadCellHanger & strLoadCellSubEquip & _
    4.          strAddPrinter & strNetBoxBasic620 & strNetBoxBasic880 & strNetBoxDropDown & _
    5.         strPressureFilter & strRearLightProtection & strRevBuzzer & strRevCamera & strBeacon & _
    6.         strHyTower & strWingSPRearBogie & strWingSRearBogie & strWingPRearAxle & strWingPRearBogie & _
    7.         strWingPRearSteer & strToolbox & strWinch & strWorkLightSingle & strWorkLightTwin & strFinishPaintEquip

    The method works, but this seems a very long winded and disorganised way of doing things. With a previous and similar problem DKenny kindly provided a solution "building a table on the fly." However, in this instance I have to assign unique price to each item as well so can't see how I can apply that solution.

    Any suggestions or pointers would be most welcome.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  2. #2
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: There's got to be a better way...

    New2vba:

    You are my new hero!

    I can't imagine anyone having the persistence and perseverance to write this code.

    I don't really know what you are doing or trying to do, but it sure looks to me like there certainly MUST be a better way.

    I don't know how you will ever maintain your code and what will you do about price changes or name changes.

    You have a worklightsingle and a worklighttwin. What happens when someone decides to change the name of worklighttwin to worklightdouble?

    Isn't it time you moved your data into some kind of database (Access, SQL, even an old copy of dBase III) and created some kind of front end application in Visual Basic 6.0 or .Net.

    I would think that even writing your complete application in Access would be much better than what you are currently doing.

    Like I said, I don't really know what you are doing or tyring to do, but you are my new HERO!

    Good Luck

  3. #3

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: There's got to be a better way...

    Unfortunately AIS4U the application must be created solely in Word/vba (not that I would know how to do it with VB or in a dbase anyway )

    I am creating a series of quotations for equipment that is technical in nature. The items in the list will vary depending on what type of equipment has been chosen and what options are included with the equipment. Each quotation template employs a userform containg a series of textboxes, comboboxes and checkboxes.

    To aid with price changes, I have created a price list containing strSomeItemPrice for each item so updating prices (may occur once or twice a year) can be done in the VBE. Changing of names is highly unlikely. To explain, take for example, a car - a windscreen is called a basic windscreen and no one is likely to change it anytime soon. The total number of options is also unlikely to increase anytime soon.

    One possible direction (kindly shown to me by Dkenny) would be to use an array that would be populated with items that meet my conditions, i.e. enabled and false. However, I don't know how to assign the price to each item in the array.

    Still clueless
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  4. #4
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: There's got to be a better way...

    hello,

    I've been looking at your code and it could be made a lot more efficient.

    VB Code:
    1. 'you define an array of all your prices and string variables
    2. dim x as integer
    3. Dim MainPriceControlArray(5, 2) As String
    4.     MainPriceControlArray(1, 1) = "100.00" '    strProtectionCoversPrice = "100.00"
    5.     MainPriceControlArray(2, 1) = "200.00" '    strChainShortenersPrice = "200.00"
    6.     MainPriceControlArray(3, 1) = "300.00" '    strDetachTopBarPrice = "300.00"
    7.     MainPriceControlArray(4, 1) = "400.00" '    strFireExtingExtPrice = "400.00"
    8.     MainPriceControlArray(5, 1) = "500.00" '    strFireExtingIntPrice = "500.00"
    9.     MainPriceControlArray(1, 2) = "strProtectionCovers" '    strProtectionCovers
    10.     MainPriceControlArray(2, 2) = "strChainShorteners" '    strChainShorteners
    11.     MainPriceControlArray(3, 2) = "strDetachTopBar" '    strDetachTopBar
    12.     MainPriceControlArray(4, 2) = "strFireExtingExt" '    strFireExtingExt
    13.     MainPriceControlArray(5, 2) = "strFireExtingInt" '    strFireExtingInt
    14. 'then you loop through your array and controls in the document
    15. ' you check each  control for it's anabled and value property and take the appropriate action
    16.  
    17.     For x = 1 To UBound(MainPriceControlArray)
    18.         For Each ctl In doc.Controls 'doc is the document you're working in
    19.             If ctl.Enabled = True And ctl.Value = False Then
    20.                     doc.Controls(MainPriceControlArray(x, 2)) = vbCr & ctl.Caption & vbTab & "$" & vbTab & MainPriceControlArray(x, 1)
    21.             End If
    22.         Next ctl
    23.     Next

    I think this will get you on your way

  5. #5

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: There's got to be a better way...

    Thanks for the help Killazzz, although I can't get the code to work.

    Just to clarify, all my controls are on a userform and when OK is pressed, my final list (amongst other things) is inserted into the document.

    The insertion point for the list is a bookmark and it ends up like this:

    Item1 £ 100.00
    Item2 £ 200.00
    Item3 £ 400.00
    Item4 £ 900.00
    Item5 £ 150.00
    etc...

    Any additional pointers would be most welcome.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  6. #6
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: There's got to be a better way...

    Hello,

    Can you post the doc ? That way I can alter your code and you can see how it can be done ?

    kind regards,
    Jason

  7. #7

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: There's got to be a better way...

    I've not yet tried the code in my actual document. Before implementing it I added it to a document on it's own to try and understand it a little better.

    The code has been slightly modified from original because I could'nt get it to work, although it still doesn't work - I'm simply adding Array(5,2) to my document.

    Any help would be appreciated.
    Attached Files Attached Files
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  8. #8
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: There's got to be a better way...

    VB Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3. 'you define an array of all your prices and string variables
    4. Dim A As Variant
    5. Dim x As Integer
    6. Dim chkMyCheck As Control
    7. Dim MainPriceControlArray(5, 2) As String
    8.  
    9.     MainPriceControlArray(1, 1) = "100.00" '    strCheckBox1 = "100.00"
    10.     MainPriceControlArray(2, 1) = "200.00" '    strCheckBox2 = "200.00"
    11.     MainPriceControlArray(3, 1) = "300.00" '    strCheckBox3 = "300.00"
    12.     MainPriceControlArray(4, 1) = "400.00" '    strCheckBox4= "400.00"
    13.     MainPriceControlArray(5, 1) = "500.00" '    strCheckBox5 = "500.00"
    14.    
    15.     [B]MainPriceControlArray(1, 2) = "textbox1" '    strProtectionCovers[/B]
    16.     MainPriceControlArray(2, 2) = "strCheckBox2" '    strChainShorteners
    17.     MainPriceControlArray(3, 2) = "strCheckBox3" '    strDetachTopBar
    18.     MainPriceControlArray(4, 2) = "strCheckBox4" '    strFireExtingExt
    19.     MainPriceControlArray(5, 2) = "strCheckBox5" '    strFireExtingInt
    20.    
    21. 'then you loop through your array and controls in the document
    22. ' you check each  control for it's anabled and value property and take the appropriate action
    23.  
    24.     For x = 1 To UBound(MainPriceControlArray)
    25.         For Each chkMyCheck In Me.Controls
    26.             If [B]LCase(Left(chkMyCheck.Name, 3)) = "che"[/B] Then
    27.                 If chkMyCheck.Enabled = True And chkMyCheck.Value = False Then
    28.                     [B]Me.Controls(MainPriceControlArray(x, 2)) [/B] = vbCr & chkMyCheck.Caption & vbTab & "$" & vbTab & MainPriceControlArray(x, 1)
    29.                 Exit For
    30.                 End If
    31.             End If
    32.         Next chkMyCheck
    33.     Next
    34.    
    35.  
    36.  
    37.     With ActiveDocument
    38.     .Bookmarks("Test").Range.Text = MainPriceControlArray(5, 2)
    39.     End With
    40. End Sub

    the bold sections I've changed
    Attached Files Attached Files

  9. #9

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: There's got to be a better way...

    Killazzz,

    I don't know if it's something I'm doing, but this doesn't seem to work. Unless I check checkbox 5, I simply get an error.

    And if I do check checkbox5, only that value appears at my bookmark.

    What I am trying to do is generate a list at my bookmark, which contains all items that are enabled yet false. For example, if checkbox1 and checkbox2 were the only boxes left that were both enabled and unchecked, then I would get:

    Checkbox1.caption "£" Checkbox1 item price
    Checkbox2.caption "£" Checkbox2 item price

    And there would be tabs in between the caption, "£" and the price (the tab positions are built into the style in the word document).

    Any ideas how I might resolve this?
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  10. #10
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: There's got to be a better way...

    This should do the trick
    Attached Files Attached Files

  11. #11

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: There's got to be a better way...

    Works perfectly Killazz (although it'll probably take a while for me to fully appreciate what it's doing )

    Thanks a lot for your help.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

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