Results 1 to 4 of 4

Thread: Shorten this code with an array?

  1. #1
    Guest

    Question

    This is an exerpt of my code, it goes on like this 9 times! Ugh! I know I can shorten it by useing an array or an index but I havn't a clue how to make that work. Anyone feel like helping me?

    Public Function MultiDirExport(ExportPath)

    Dim DirExport As String

    If frmMultiDir.txtDir(0).Text = "" Then
    Exit Function
    Else
    DirExport = frmMultiDir.txtDir(0).Text
    DosShell "dir /b " & DirExport & " >> C:\Windows\Temp\MP3_Export.txt"
    End If

    If frmMultiDir.txtDir(1).Text = "" Then
    Exit Function
    Else
    DirExport = frmMultiDir.txtDir(1).Text
    DosShell "dir /b " & DirExport & " >> C:\Windows\Temp\MP3_Export.txt"
    End If

    If frmMultiDir.txtDir(2).Text = "" Then
    Exit Function
    Else
    DirExport = frmMultiDir.txtDir(2).Text
    DosShell "dir /b " & DirExport & " >> C:\Windows\Temp\MP3_Export.txt"
    End If

    End Function

  2. #2
    Guest
    Hello Yogi_Bear_79,

    You could do it this way:
    Code:
    Public Function MultiDirExport(ExportPath)
    
    Dim DirExport As String
    Dim I As Integer
    
      For I = 0 To frmMultiDir.txtDir.Count
        If Len(frmMultiDir.txtDir(I).Text) > 0 Then
          DirExport = frmMultiDir.txtDir(I).Text
          DosShell "dir /b " & DirExport & " >> C:\Windows\Temp\MP3_Export.txt"
        End If
      Next I
    
    End Function
    Hope this helps,

  3. #3
    Lively Member
    Join Date
    Jun 2000
    Location
    Belgium
    Posts
    77
    Your wrong Rwa, because Yogi_Bear_79 exit function after the first txtDir(i)="". The code look like this
    Code:
    Public Function MultiDirExport(ExportPath) 
    
    Dim DirExport As String 
    Dim I as integer
    dim MaxI as integer
    Maxi=frmMultiDir.txtDir.Count
    
    For i=0 to MaxI
     If frmMultiDir.txtDir(0).Text = "" Then 
        Exit Function 
        Else 
        DirExport = frmMultiDir.txtDir(0).Text 
        DosShell "dir /b " & DirExport & " >> C:\Windows\Temp\MP3_Export.txt" 
     End If 
    Next
    
    End Function
    KWell

  4. #4
    Guest
    Well there KWell,

    Your code will only check one textbox:

    If frmMultiDir.txtDir(0).Text = "" Then

    Then finish looping while checking nothing. I think my code will work a little better. It will write to the log file if any of the textbox's have text in them.

    Best,

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