Load .txt file into ListView

dboone25

Board Regular
Joined
May 8, 2015
Messages
185
Good morning all. Have a query regarding the ListView and loading a.txt file straight into the ListView.

I have a userform with one button which opens the load file dialoge box but i am unsure how to show the contents of a text file into the ListView.

My code below is to select a file.

Code:
' Declare variables.Dim myFile As String
Dim FileName As Variant
Dim r As Range
Dim i As Long
Dim rc As Range
Dim rr As Long




' Select CSV file.
' Open dialog box title.
' Add title to open file dialogue box " Select .csv file to import


myFile = "TXT Files (*.txt),*.txt"
'Title = "Select .TXT File to Import"
FileName = Application.GetOpenFilename(Title:="Select .txt File to Import")
                                                                          
' Select and open the dialog box.
' If no file selected open dialog box.
If FileName = False Then
MsgBox "No File Selected"
Exit Sub
End If


Is it possible to then connect to a SQL Server which uses a text file from the ListView and output to sheet1.

I have written the connection and run a short sql query.


Code:
Private Sub Connectsqlserver()





    Dim Conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String
 
    ' Create the connection string.
    sConnString = "Provider=SQLOLEDB;Data Source=Database1;" & _
                  "Initial Catalog=CommandTables;"
                  
    
    ' Create the Connection and Recordset objects.
    Set Conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    ' Open the connection and execute.
    Conn.Open sConnString
    Set rs = Conn.Execute("SELECT * FROM TableInfo;")
    
    ' Check we have data.
    If Not rs.EOF Then
        ' Transfer result.
        Sheets(1).Range("A1").CopyFromRecordset rs
    ' Close the recordset
        rs.Close
    Else
        MsgBox "Error: No records returned.", vbCritical
    End If


    ' Clean up
    If CBool(Conn.State And adStateOpen) Then Conn.Close
    Set Conn = Nothing
    Set rs = Nothing
End Sub


Can this be done?

Reason why I would like to run an text file into ListView and then run the SQL script from ListView into Sheet1 as this will make it more user friendly to edit anyinformation found in ListView.
 
FileName is the variable that will hold the path and name for the file the user selects here.
Code:
FileName = Application.GetOpenFilename(Title:="Select .txt File to Import")
This code is totally unneeded.
Code:
  If Dir(sPath) = "" Then
        MsgBox "File was not found."
        Exit Sub
    End If
    Close
This code,
Code:
Open sPath For Input As #1
    Do Until EOF(1)
        Line Input #1, sTxt
        sText = sText & sTxt & vbLf
    Loop

    Close
should be changed to this.
Code:
Open FileName For Input As #1
    Do Until EOF(1)
        Line Input #1, sTxt
        sText = sText & sTxt & vbLf
    Loop

    Close #1
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,216,471
Messages
6,130,822
Members
449,595
Latest member
jhester2010

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top