Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

inserting text file into mdb not working... help please

Posted on 2006-03-26
12
Medium Priority
?
272 Views
Last Modified: 2008-02-26
This has not been my day:-(... I am having problems updating an access table (Points) with text data that was pulled in (Open file dialog 1). Below is the code... I have gotten help with this part of the code but am once again, neck deep in the mud. Thanks for the help

Code:
    Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Dim cn As New ADODB.Connection
        'Dim PathtoTextFile As String
        'Dim PathtoMDB As String
        Dim sFilePath2 As String
        sFilePath2 = OpenFileDialog2.FileName
        'PathtoTextFile = "& sFilePath2 """
        'PathtoMDB = "& sFilePath2 """
        Dim da As New OleDbDataAdapter
        Dim ds As New DataSet
        Dim sAppPath As String
        Dim dbPath As String
        Dim sDBName As String
        sAppPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
        sDBName = "//PntCon_V2.mdb"
        dbPath = "" & sAppPath & sDBName & ""
        'cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _
        '          "Data Source=" & sAppPath & sDBName & "")

        'cn.Execute("INSERT INTO Points SELECT * FROM " & _
        '             "[Text;Database=" & sFilePath2 & ";HDR=YES],")

        'cn.Close()
        'Private Sub DefineAdapter()
        Dim sFilePath1 As String
        sFilePath1 = OpenFileDialog1.FileName
        'Dim dbPath As String
        'Dim sDBName As String
        'Dim sAppPath As String
        sAppPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
        sDBName = "//PntCon_V2.mdb"
        dbPath = "" & sAppPath & sDBName & ""
        With da
            .SelectCommand = New OleDb.OleDbCommand
            With .SelectCommand
                .Connection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sFilePath1 & ";Extended Properties=text")  'either define sfilepath1 prior to calling this procedure or create the connection object separately
                .CommandText = "SELECT * FROM " & sFilePath1 & ""
            End With

            .InsertCommand = New OleDb.OleDbCommand
            With .InsertCommand
                .Connection = New OleDb.OleDbConnection(("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sAppPath & sDBName & ""))
                .CommandText = "INSERT INTO Points(Field1,field2,field3,field4,field5) VALUES (ds.f1, ds.f2, ds.f3, ds.f4, ds.f5)"
                'With .Parameters
                '    .Add("Field1", OleDbType.Double, 20, "f1") ' field 1 is the column in the points table that i want the f1 values to go to
                '    .Add("field2", OleDbType.Double, 20, "f2") ' field 2 is the column in the points table that i want the f2 values to go to
                '    .Add("field3", OleDbType.Double, 20, "f3")
                '    .Add("field4", OleDbType.Double, 20, "f2")
                '    .Add("field5", OleDbType.VarChar, 25, "f3")
                '    'repeat for other 4 columns
                'End With
            End With

            .AcceptChangesDuringFill = False 'maintains their rowstate as new so that they can immediately inserted with the update method
            .TableMappings.Add("ds", "Points") 'Not sure about here
            da.Fill(ds)
            ' ''define connection for .insertcommand
            ' ''create the access database
            da.Update(ds)
        End With
0
Comment
Question by:JohnEddins
12 Comments
 

Author Comment

by:JohnEddins
ID: 16300264
Thanks for looking but the above links are not helping me out a much...I am still having problems, it seems from everything I have read that this should be easy, but I must be even bumber then I thought cause i can not get this thing to work!
0
 

Author Comment

by:JohnEddins
ID: 16300318
Below is my code: I am getting an error at the cn.execute line... Any thoguhts?    

Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Dim cn As New ADODB.Connection
        'Dim PathtoTextFile As String
        'Dim PathtoMDB As String
        Dim sFilePath2 As String
        sFilePath2 = OpenFileDialog2.FileName
        'PathtoTextFile = "& sFilePath2 """
        'PathtoMDB = "& sFilePath2 """
        Dim da As New OleDbDataAdapter
        Dim ds As New DataSet
        Dim sAppPath As String
        Dim dbPath As String
        Dim sDBName As String
        sAppPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
        sDBName = "//PntCon_V2.mdb"
        dbPath = "" & sAppPath & sDBName & ""
        cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                  "Data Source=" & sAppPath & sDBName & "")

        cn.Execute("INSERT INTO Points SELECT * FROM " & _
                     "[Text;Database=" & sFilePath2 & ";HDR=YES],")

        cn.Close()
        'Private Sub DefineAdapter()
        Dim sFilePath1 As String
        sFilePath1 = OpenFileDialog1.FileName
        sAppPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
        sDBName = "//PntCon_V2.mdb"
        dbPath = "" & sAppPath & sDBName & ""
        With da
            .SelectCommand = New OleDb.OleDbCommand
            With .SelectCommand
                .Connection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sFilePath1 & ";Extended Properties=text")  'either define sfilepath1 prior to calling this procedure or create the connection object separately
                .CommandText = "SELECT * FROM " & sFilePath1 & ""
            End With

            .InsertCommand = New OleDb.OleDbCommand
            With .InsertCommand
                .Connection = New OleDb.OleDbConnection(("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sAppPath & sDBName & ""))
                .CommandText = "INSERT INTO Points(F1,f2,f3,f4,'f5') VALUES (ds.f1, ds.f2, ds.f3, ds.f4, 'ds.f5')"
                With .Parameters
                    .Add("F1", OleDbType.Double, 20, "f1") ' field 1 is the column in the points table that i want the f1 values to go to
                    .Add("F2", OleDbType.Double, 20, "f2") ' field 2 is the column in the points table that i want the f2 values to go to
                    .Add("F3", OleDbType.Double, 20, "f3")
                    .Add("F4", OleDbType.Double, 20, "f2")
                    .Add("F5", OleDbType.VarChar, 25, "f3")
                    '    'repeat for other 4 columns
                    'End With
                End With
                da.AcceptChangesDuringFill = False 'maintains their rowstate as new so that they can immediately inserted with the update method
                da.TableMappings.Add("ds", "Points") 'Not sure about here
                da.Fill(ds)
                ' ''define connection for .insertcommand
                ' ''create the access database
                da.Update(ds)
            End With
        End With

    End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:JRossi1
ID: 16302575
Knowing what the error message your getting would help.

Thanks.
0
 
LVL 6

Expert Comment

by:JRossi1
ID: 16302625
Try this:

Sub ImportTextToAccess()

      Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=d:\My Documents\db1.mdb")

      AccessConn.Open()

      'New table - This will create the table if it doesn't exist
      Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [tbl1] FROM " & _
      "[Text;DATABASE=d:\My Documents\TextFiles].[CSV.txt]", AccessConn)

      'Existing table
      Dim AccessCommand As New System.Data.OleDb.OleDbCommand("INSERT INTO [urTable] (F1, F2, F3, F4,
      F5)  SELECT F1, F2, F3, F4, F5 FROM [Text;DATABASE=d:\My Documents\TextFiles;].[CSV.txt]",  AccessConn)

      AccessCommand.ExecuteNonQuery()
      AccessConn.Close()

End Function


0
 

Author Comment

by:JohnEddins
ID: 16302812
Sorry for the delay....
I modified what you gave me above to look like this.... and it generated an error message at the accessCommand.executeNonQuery Line: Error says there is a problem in the from statment... Abyhow, below is wha ti chang ewhat you provided to look like, and thanks for your time with this::
        Dim sAppPath As String
        Dim dbPath As String
        Dim sDBName As String
        Dim sfilepath1 As String
        sAppPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
        sDBName = "//PntCon_V2.mdb"
        dbPath = "" & sAppPath & sDBName & ""
        Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & sAppPath & sDBName & "")

        AccessConn.Open()

        'New table - This will create the table if it doesn't exist
        'Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [tbl1] FROM " & _
        '"[Text;DATABASE=" & sfilepath1 & "].[CSV.txt]", AccessConn)
  ' Above line commented out beacuse i create the database and the table on the form load event. So I know its there
        'Existing table
        Dim AccessCommand As New System.Data.OleDb.OleDbCommand("INSERT INTO [Points] (F1, F2, F3, F4, F5)  SELECT F1, F2, F3, F4, F5 FROM [Text;DATABASE=" & sfilepath1 & "", AccessConn)

        AccessCommand.ExecuteNonQuery()
        AccessConn.Close()
    End Sub
0
 
LVL 6

Expert Comment

by:JRossi1
ID: 16305699
Try hardcoding the path to the text file:

 Dim AccessCommand As New System.Data.OleDb.OleDbCommand("INSERT INTO [Points] (F1, F2, F3, F4, F5)  SELECT F1, F2, F3, F4, F5 FROM [Text;DATABASE=C:\MytextFile.txt", AccessConn)

Want to see if the sFilePath variable is being correctly populated.
0
 

Author Comment

by:JohnEddins
ID: 16310125
I was traveling last night, but Im back now... i just gave what you suggest a shot 9hasrd code the text path,a dn I am sitll getting an error at the AccessCommand.Excute string for an error in the form clause...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16311125
What exception are you getting now?

Bob
0
 
LVL 6

Accepted Solution

by:
JRossi1 earned 2000 total points
ID: 16311271
I was able to get this to work by changing the SQL string to this:

 Dim AccessCommand As New System.Data.OleDb.OleDbCommand("INSERT INTO TableName (F1, F2, F3 ) SELECT * FROM [Book1.txt] IN 'C:\Data'", AccessConn)

I think you have to separate the file name from the path.  
0
 

Author Comment

by:JohnEddins
ID: 16311319
JRoss1
That got it... thanks for both your help and time!
John
0
 
LVL 6

Expert Comment

by:JRossi1
ID: 16311441
No problem.  That was a bit tricky.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question