borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (help with Access Email procedure..)Next Topic (tooltip and embeded button in excel) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Procedure too larger error??
Poster Message
desdee
Level: Guest


icon Procedure too larger error??

Can anyone tell me if there is a way of dividing up my procedure without loosing the current functionality. The following is my code for three boxes on my form (2 combo and 1 list box). each box is populated depending on what the user selects in the previous box. This has worked fine, until I increased the number of selections in each list. My sample code is as follows (there are actually 80 RepNames, which can trigger approx 50 postcodes each and so on...

vb/
Private Sub Name_Enter()
On Error Resume Next
RepName.Clear
RepName.AddItem "Name1" '0
RepName.AddItem "Name2" '1
RepName.AddItem "Name3" '2

End Sub

Private Sub RepName_Click()
On Error Resume Next
If RepName.Selected(0) Then
Postcode.Clear
Postcode.AddItem "AB10"
Postcode.AddItem "AB11"
Postcode.AddItem "AB12"
Postcode.AddItem "AB13"
Postcode.AddItem "AB14"
Postcode.AddItem "AB15"
Postcode.AddItem "AB16"
Postcode.AddItem "AB21"
Postcode.AddItem "AB22"
Postcode.AddItem "AB23"
Postcode.AddItem "AB24"
Postcode.AddItem "AB25"
Postcode.AddItem "AB31"
Postcode.AddItem "AB32"
Postcode.AddItem "AB30"
Postcode.AddItem "AB33"
Postcode.AddItem "AB34"
Postcode.AddItem "AB35"
Postcode.AddItem "AB36"
Postcode.AddItem "AB37"
Postcode.AddItem "AB39"
Postcode.AddItem "AB41"
Postcode.AddItem "AB42"
Postcode.AddItem "AB43"
Postcode.AddItem "AB44"
Postcode.AddItem "AB45"
Postcode.AddItem "AB51"
Postcode.AddItem "AB52"
Postcode.AddItem "AB53"
Postcode.AddItem "AB54"
Postcode.AddItem "AB55"
Postcode.AddItem "AB56"
Postcode.AddItem "DD 8"
Postcode.AddItem "DD 9"
Postcode.AddItem "DD10"
Postcode.AddItem "PH10"
Postcode.AddItem "PH11"
Postcode.AddItem "PH18"
Postcode.AddItem "PH21"
Postcode.AddItem "PH22"
Postcode.AddItem "AB38"


SalesType.Clear
SalesType.AddItem "Hardware"
SalesType.AddItem "Software"
SalesType.AddItem "Books"
SalesType.AddItem "Magazines"


End If

End Sub
/vb

24-02-2004 at 04:45 PM
| Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Procedure too larger error??

Well, I am not sure what exactly do you do with this code, but if you want to make your procedure smaller then you can use For... Next, altought it will make reading code harder. This code will insert all AB** items in your RepName_Click procedure.

        For inCnt = 10 To 56
            PostCode.AddItem "AB" & inCnt
            If inCnt = 16 or inCnt = 25 Then
                inCnt = inCnt + 4
            ElseIf inCnt = 39 Then
                inCnt = inCnt + 1
            ElseIf inCnt = 45 Then
                inCnt = 50
            End If
        Next inCnt


Next, I dont see the point in clearing combobox and adding SAME data in PostCode everytime RepName is clicked. Also the same thing with SalesType. You are adding the same data everytime when you click on RepName, while you can do it only once. As, I said, I dont quite understand what exactly are you trying to achieve, but this is surely not well organised code. Maybe you can store all this items from AB10 to PH22 to some file, so you can read it and load it to combobox with much less code.

____________________________
If you find the answer helpful, please mark this topic as solved.

24-02-2004 at 08:22 PM
View Profile Send Email to User Show All Posts | Quote Reply
desdee
Level: Guest

icon Re: Procedure too larger error??

thanks for responding, I don't think I was clear before, each of the sales reps have a different county/ area to deal with, so the postcodes always change when a new sales rep name is selected, so for example name3 may work in Scotland, so only postcodes in Scotland will appear in the second list -the list is populated differently each time, for each of the boxes. I have to appologise as I am new to all this, and am trying very hard to get to grips with it. I will try what you have suggested - but how do I store all these items in my lists to a file and read it and load it to combobox with much less code?

your help is much appreciated!

24-02-2004 at 09:09 PM
| Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Procedure too larger error??

Search this forum, there is plenty of code about writing to a file. Also, you might want to reconsider organising your data in tables (I suggest that you already have a database), its much more easier to work with tables instead with sequential files. I will post here 2 small examples how to read and write to a sequential file, and for more info, search this forum or MSDN.

Private Sub WriteToFile()
Dim ff As Integer
    
    ff = FreeFile
    ' Open the file
    Open "Testfile" For Output As #ff
    
    Write #ff, "AB10"
    Write #ff, "AB11"
    Write #ff, "AB12"
    '..................

Close #ff
End Sub

Private Sub ReadFromFile()
Dim ff As Integer
Dim str As String

    ff = FreeFile
    ' Open the file
    Open "TestFile" For Input As #ff
    
    Do While Not EOF(ff)
        Input #ff, str
        Combo1.AddItem str
    Loop
    
Close #ff
End Sub


First call WriteToFile procedure to create file 'TestFile' and to add data to it, and then call ReadFromFile procedure to read data from it and, as an example, I added line that will display data that you read in ComboBox control named Combo1.

____________________________
If you find the answer helpful, please mark this topic as solved.

24-02-2004 at 10:19 PM
View Profile Send Email to User Show All Posts | Quote Reply
desdee
Level: Guest

icon Re: Procedure too larger error??

Thanks to all who contributed, I managed to solve my problem in the end by using sub procedures and calling them.

Cheers everyone  

22-03-2004 at 03:13 PM
| Quote Reply
desdee
Level: Guest

icon Re: Procedure too larger error??

Thanks to all who contributed, I managed to solve my problem in the end by using sub procedures and calling them.

Cheers everyone  

22-03-2004 at 03:13 PM
| Quote Reply
neutrall
Level: Master


Registered: 28-03-2004
Posts: 122
icon Re: Procedure too larger error??

Using Sub and function are always a good move.

And If you are always using the same sequence, you may use a for loop : EX

For X = 1 to 10 step 1
  CmbBox1.AddItem "AB" & X
next X



DAniel B.

____________________________
A Stick give a wise man something to think about... and a fool, something to put in is mouth.

30-03-2004 at 04:02 AM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Procedure too larger error??
Previous Topic (help with Access Email procedure..)Next Topic (tooltip and embeded button in excel) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder