Link to home
Start Free TrialLog in
Avatar of ukkrew
ukkrew

asked on

Convert VB6 Listbox Sort to Array Sort...

I have the following code...


==========


Option Explicit

Private Sub cmdScan_Click(Index As Integer)
MsgBox "Working on file " & cmdScan(Index).Caption
End Sub

Private Sub Command1_Click()
    Dim ScanYear As String
    Dim ScanFormat  As String
    Dim ScanPath  As String
    Dim ScanMaxDocs  As String
    Dim ScanReference As String
    Dim strFileName As String
    Dim i As Integer
    Dim strScans() As String
   
    ScanYear = "2006"
    ScanFormat = ".pdf"
    ScanPath = "C:\Scans\"
    ScanMaxDocs = "6" '(this is currently the maxiumum pages to a series of documents)
    ScanReference = "12345678"
    Me.List1.Clear
    For i = 0 To 5
        cmdScan(i).Visible = False
    Next i
    strFileName = Dir$(ScanPath & ScanReference & "-" & ScanYear & "-" & "*" & ScanFormat)
    Do Until strFileName = ""
        List1.AddItem strFileName
        strFileName = Dir$()
    Loop
    For i = 0 To List1.ListCount - 1
        cmdScan(i).Visible = True
        cmdScan(i).Caption = List1.List(i)
    Next i
End Sub

Private Sub Form_Load()
Dim i As Integer
    Me.List1.Clear 'Me means this Form. It can be omitted here.
    For i = 0 To 5
        Me.cmdScan(i).Visible = False
    Next i
End Sub


==========


I'd like some help converting this to an access "safe" array sort rather than a sort in a listbox.

Many thanks!
Avatar of rockiroads
rockiroads
Flag of United States of America image

something like this perhaps


Private Sub Command1_Click()
   
    Dim ScanYear As String
    Dim ScanFormat  As String
    Dim ScanPath  As String
    Dim ScanMaxDocs  As String
    Dim ScanReference As String
    Dim strFileName As String
    Dim i As Integer
    Dim strScans() As String
    Dim bLoop As Boolean
   
    ScanYear = "2006"
    ScanFormat = ".pdf"
    ScanPath = "C:\Scans\"
    ScanMaxDocs = "6" '(this is currently the maxiumum pages to a series of documents)
    ScanReference = "12345678"
   
    Dim sDocs(MAX_DOCS)
    'Initialise
    For i = 0 To UBound(sDocs)
        cmdScan(i).Visible = False
    Next i
   
    strFileName = Dir$(ScanPath & ScanReference & "-" & ScanYear & "-" & "*" & ScanFormat)
   
    i = 0
    bLoop = True
    Do While bLoop = True
        If strFileName = "" Then
            bLoop = False
        ElseIf i > UBound(sDocs) Then  'no more room
            MsgBox "No More Room"
            bLoop = False
        Else
            sDocs(i) = strFileName
        End If
        strFileName = Dir$()
        i = i + 1
    Loop
   
    For i = 0 To UBound(sDocs)
        cmdScan(i).Visible = True
        cmdScan(i).Caption = sDocs(i)
    Next i
End Sub
forgot Const MAX_DOCS = 6


alternative is to use a collections class
but above is easy enough
oh, your code, I forgot u cannot have array of controls, it has to be individual controls

plus I forgot the constants

say u names your command buttons

cmdScan0,cmdScan1,cmdScan2,...cmdScan5

Const MAX_DOCS = 6  '(this is currently the maxiumum pages to a series of documents)
Dim sDocs() As String


Private Sub Command1_Click()
   
    Dim ScanYear As String
    Dim ScanFormat  As String
    Dim ScanPath  As String
    Dim ScanReference As String
    Dim strFileName As String
    Dim i As Integer
    Dim strScans() As String
    Dim bLoop As Boolean
   
    ScanYear = "2006"
    ScanFormat = ".pdf"
    ScanPath = "C:\Scans\"
    ScanReference = "12345678"
   
    'Initialise
    For i = 0 To MAX_DOCS - 1
        Me("cmdScan" & i).Visible = False
    Next i
   
    strFileName = Dir$(ScanPath & ScanReference & "-" & ScanYear & "-" & "*" & ScanFormat)
   
    i = 0
    bLoop = True
    Do While bLoop = True
        If strFileName = "" Then
            bLoop = False
        ElseIf i > UBound(sDocs) Then  'no more room
            MsgBox "No More Room"
            bLoop = False
        Else
            sDocs(i) = strFileName
        End If
        strFileName = Dir$()
        i = i + 1
    Loop
   
    For i = 0 To MAX_DOCS - 1
        Me("cmdScan" & i).Visible = True
        Me("cmdScan" & i).Caption = sDocs(i)
    Next i
End Sub

Private Sub Form_Load()
    Dim i As Integer
   
    ReDim sDocs(MAX_DOCS)
   
    For i = 0 To MAX_DOCS - 1
        Me("cmdScan" & i).Visible = False
    Next i
End Sub


Avatar of ukkrew
ukkrew

ASKER

thanks, rockiroads...

I'm getting a few errors trying out the code...

if no files / directory not found, i get: run time error 5

if files exist (tried with 1), i get: run time error 9

any ideas?

Many thanks.
no files

ok, check for file before loop i.e.

strFileName = Dir$(ScanPath & ScanReference & "-" & ScanYear & "-" & "*" & ScanFormat)

if strFileName <> "" then

    i = 0
    bLoop = True
    Do While bLoop = True
        If strFileName = "" Then
            bLoop = False
        ElseIf i > UBound(sDocs) Then  'no more room
            MsgBox "No More Room"
            bLoop = False
        Else
            sDocs(i) = strFileName
        End If
        strFileName = Dir$()
        i = i + 1
    Loop
end if

ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ukkrew

ASKER

Thats great!!

The only problem now appears to be with making the buttons visible and invisible... it just doesn't seam to work?

Many thanks!!
ok, how do u want the buttons displayed?

are there always 6 documents therefore always 6 buttons displayed?

if so, then why is there code that initially hides the buttons

Or do the number vary, in that case what/when/how do u want buttons displayed

Ive done it to your code logic, but if u tell me in general what u want doing then we can address it
Avatar of ukkrew

ASKER

Not a problem rockiroads,

What i'd like is to have all the buttons initally invisible, then as the code loops through, i'd like to only make the buttons with a caption (where a file is found) made visible... Its just to try and keep things as simple as possible for the user.

Is it also possible to have some code like:

    If i = 1 Then
        ScanMsg.Caption = "There is one file in the folder"
    Else
        ScanMsg.Caption = "There are " & i & " files in the folder"
    End If

I tried putting this in, but it appears that i = 1 even when 0 files are found?

Many thanks.
Avatar of ukkrew

ASKER

don't worry rockiroads, i sussed it.

Thanks for the help!!
sorry, when at work I dont get notifications as I cant check my personal email

So I see u sussed it, cool

Hopefully wasnt a big deal