因此,我很新,在訪問中使用vba,我無法使此代碼正常工作。它應該做的是採取一個選定的文本文件,並將原始文件讀入列表框中。然後有第二個按鈕,當按下按鈕時,將文本文件從管道分隔文件轉換爲製表符分隔文件,然後將更改後的文件顯示到新的列表框中。如何將管道分隔文件轉換爲製表符分隔文件並在列表框中顯示結果VBA
Option Compare Database
Option Explicit
Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'
If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
End If
End Function
Private Sub Command0_Click()
Dim fdlg As Office.FileDialog
Dim pipe_file As Variant
Dim FileName As String
Dim fn As Integer
Dim varFile As Variant
Dim FilePath As String
Me.OrigFile.RowSource = ""
Me.ConvertFile.RowSource = ""
Me.FileName = ""
Me.FilePath = ""
FileName = ""
Set fdlg = Application.FileDialog(msoFileDialogFilePicker)
With fdlg
.AllowMultiSelect = False
.Title = "Select pipe delimited file"
.Filters.Clear
.Filters.Add "Text Files", "*.txt"
If .Show = True Then
For Each varFile In .SelectedItems
FileName = GetFilenameFromPath(varFile)
FilePath = varFile
Next varFile
Me.FileName = FileName
Me.FilePath = FilePath
fn = FreeFile
Open FileName For Input As #fn
Do While Not EOF(fn)
Line Input #fn, pipe_file
Me.OrigFile.AddItem pipe_file
Loop
Close #fn
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
Private Sub Convert_File_Click()
'ByVal OutputFile As String)'
On Error GoTo error1
Dim pipe_file As Variant
Dim ThisString As String
Dim NewString As String
Dim A As Integer
Dim InputFile As String
InputFile = Me.FilePath
Open InputFile For Input As #1
Const FileName = "c:\outputfile.txt"
Dim my_filenumber As Integer
my_filenumber = FreeFile
Open FileName For Output As #2
'Open OutputFile For Output As #2'
While Not EOF(1)
NewString = ""
Line Input #1, ThisString
For A = 1 To Len(ThisString)
If Mid(ThisString, A, 1) = "|" Then
NewString = NewString & Chr$(9)
Else
NewString = NewString & Mid(ThisString, A, 1)
End If
Next
Print #2, ThisString
Wend
Do While Not EOF(2)
Line Input #2, pipe_file
Me.ConvertFile.AddItem pipe_file
Loop
Close #2
Close #1
Exit Sub
error1:
Close #1
Close #2
End Sub
這是我迄今爲止現在我的問題是關於第二個按鈕或Convert_File_Click()convertfile是我試圖更新和文件路徑列表框持有文本文件的文件路徑的文本框是被選中。 任何幫助表示讚賞,謝謝!