Results 1 to 8 of 8

Thread: Importing Data

  1. #1

    Thread Starter
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439

    Question

    Hey folks,

    I'm just starting my first VB project after working in MSAccess. As you can imagine, Im missing my docmd's

    Basically, I was wondering if there is an import command/method/function (kinda like access's TransferText function), or do I have to write my data line by line?

    Cheers all

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well what are you importing the data from ?
    And then how do you want the data stored once its imported ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Well, this one is an import from a .csv file into an MSAccess table.

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Okay,
    Well to read in data from a file in VB, and then separate values separated by commas into an array you would do this :

    Code:
    Dim varString As String
    Dim varStrArray() As String
    Open "c:\something.csv" For Input As #1
        Do Until Eof(1)
            DoEvents
            Line Input #1, varString
            varStrArray = Split(varString, ",")        
        Loop
    Close #1
    Each iteration through the loop it will read in a new line from the file into the varString variable.
    Then it splits the data into an array called varStrArray.
    So you would then do something with the data in varStrArray. Perhaps something like this :

    Code:
    Dim varString As String
    Dim varStrArray() As String
    Dim i As Long
    Open "c:\something.csv" For Input As #1
        Do Until Eof(1)
            DoEvents
            Line Input #1, varString
            varStrArray = Split(varString, ",")        
            For i = 0 To UBound(varStrArray)
                Select Case i
                    Case 0:
                        RS.Fields("Something 0").Value = varStrArray(i)
                    Case 1:
                        RS.Fields("Something 1").Value = varStrArray(i)
                    Case 2:
                        RS.Fields("Something 2").Value = varStrArray(i)
                End Select                        
            Next I
        Loop
    Close #1
    The above RS and fields bit would be referring to a recordset using DAO.
    I dont like database stuff much so Im not too hot on it ...
    but most of the above code should do.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5

    Thread Starter
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Thanx man, that was the way I guessed I was gonna have to do it.

    Just wondered if there was a nice easy 1-liner like access..... =)

    Cheers for your help!

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well there is :

    Code:
    Just call the JustDoIt() function.
    
    
    
    
    
    
    Public Sub JustDoIt()
    Dim varString As String
    Dim varStrArray() As String
    Dim i As Long
    Open "c:\something.csv" For Input As #1
        Do Until Eof(1)
            DoEvents
            Line Input #1, varString
            varStrArray = Split(varString, ",")        
            For i = 0 To UBound(varStrArray)
                Select Case i
                    Case 0:
                        RS.Fields("Something 0").Value = varStrArray(i)
                    Case 1:
                        RS.Fields("Something 1").Value = varStrArray(i)
                    Case 2:
                        RS.Fields("Something 2").Value = varStrArray(i)
                End Select                        
            Next I
        Loop
    Close #1
    End Sub


    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    Junior Member AWF's Avatar
    Join Date
    Apr 2001
    Location
    United Kingdom
    Posts
    20
    You wanted a nice one liner, Well here is one.


    appAccess.OpenCurrentDatabase databasename 'Open DB

    'Import file using table specification for setting
    appAccess.DoCmd.TransferText acImportDelim, "Import Specification", _
    "Tablename ", FilePathToOpen, True
    appAccess.CloseCurrentDatabase
    appAccess.Quit

    Hope you find this OK
    'Resistance is Futile'

  8. #8

    Thread Starter
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Heheh, that's kinda cheating, but I like it!

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