Bha, è andata anche questa. Di seguito posto il codice per Aprire un file sequenziale, con matrice di controlli ed altre Txt.
Gradirei comunque qualche suggerimento per eventualmente migliorare il codice:
codice:
Dim boolVerifica1 As Boolean
Dim boolErrAnnCO1 As Boolean
Private Sub Apri_Click()
Dim TextLine
Dim y As Integer
Dim y1 As Variant
Dim Y2
Dim FileNum As Integer
Dim FileName As String
Dim FileTitle As String
Dim array_riga() As String
'Variabile di MsgBox:
Dim intDom2 As Integer
If boolVerifica1 = False And (Form1.TxtVS1(8).Text) <> "" Then
intDom2 = MsgBox("Prova Apri/Salva file sequenziale - Volete salvare il file?" & Chr(13) _
& "Clic su OK per effettuare l'operazione" & Chr(13) _
& "su Annulla per aprire un nuovo file, senza salvare quello in uso.", vbExclamation + vbOKCancel, "Prova Apri/Salva")
' Pulsante OK:
If intDom2 = 1 Then
' Invia alla routine salva FrmTariffa:
Call Form1.Salva_Click
End If
End If
With Form1.CommonDialog1
' Imposta CancelError su True:
.CancelError = True
On Error GoTo ErrHandler
.Filter = "File di programma (*.csv)|*.csv|Documenti di Testo (*.Txt)|"
.FilterIndex = 1
.DefaultExt = "csv"
.Flags = cdlOFNNoChangeDir Or cdlOFNFileMustExist Or cdlOFNNoReadOnlyReturn
.InitDir = App.Path & "\DATI\Compenso\"
.DialogTitle = "Selezionare il file da aprire"
.FileName = Form1.CommonDialog1.FileName
.ShowOpen
ErrHandler:
' E' stato scelto Annulla:
If Err.Number = 32755 Then
boolErrAnnCO1 = True
Exit Sub
Else: boolErrAnnCO1 = False
End If
' Visualizza nome e percorso del file in uso nel Titolo della Frm:
Form1.Caption = " Prova Apri\Salva - " & Form1.CommonDialog1.FileName
FileNum = FreeFile()
FileName = Form1.CommonDialog1.FileName
FileTitle = Form1.CommonDialog1.FileTitle
' Esce se l'utente ha fatto clic su annulla:
If Len(FileName) = 0 Then
Exit Sub
End If
End With
Open FileName For Input As FileNum
' Ripete fino alla fine del file:
Do While Not (EOF(FileNum) = True)
' Assegna la riga a una variabile:
Line Input #FileNum, TextLine
array_riga = Split(TextLine, ",", -1, vbTextCompare)
For y1 = 0 To 8
Form1.TxtVS1(y1).Text = array_riga(y1)
Next y1
For Y2 = 9 To 11
Form1.txtMin.Text = array_riga(9)
Form1.txtMedio.Text = array_riga(10)
Form1.txtConcil.Text = array_riga(11)
Next Y2
Loop
If (EOF(FileNum)) = False Then
Close #FileNum 'Chiude il file
MsgBox "Errore nell'apertura del file! Ripetere l'operazione.", vbExclamation + vbOKOnly, "Prova Apri/Salva"
boolVerifica1 = True
Exit Sub 'Esce dalla routine
End If
Close #FileNum
End Sub