Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to transfer text file in the Access table
Message
De
31/07/2002 03:42:21
 
 
À
31/07/2002 00:43:54
Information générale
Forum:
Visual Basic
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00683792
Message ID:
00684311
Vues:
27
Here is little sample:

1. To database access I use ADO. It is preferable - ADO is newest technology.
2. Access database is called SampleDatabase.mdb; Table in it is called SampleTable with two fields - Name (Text 20) and Age (Number Byte).
3. Text file is called SampleTextFile. It contains rows in format:
Ivan;22
George;23
Nicole;24
.
.
.
4. Data is inserted into database through SQL command INSERT INTO. In this way you do not need to open recordset, because you do not need to see the data.
5. Open Project|References. Select maximum possible version of Microsoft ActiveX Data Objects XXX Library you have installed (XXX must be 2.0 or higher).
6.Put on the form CommandButton and paste this code into its Click event:
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim CurrentLine As String
Dim SemicolonPosition As Integer
Dim NameFromTXT As String
Dim AgeFromTXT As Byte

  cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\TXT2ACCESS\SampleDatabase.mdb;Persist Security Info=False"
  cn.Open
  Set cmd.ActiveConnection = cn
  Open "c:\temp\TXT2ACCESS\SampleTextFile.txt" For Input As #1
  Do While Not EOF(1)
    Line Input #1, CurrentLine
    SemicolonPosition = InStr(1, CurrentLine, ";", vbBinaryCompare)
    NameFromTXT = Trim(Left(CurrentLine, SemicolonPosition - 1))
    AgeFromTXT = Val(Mid(CurrentLine, SemicolonPosition + 1))
    cmd.CommandText = "INSERT INTO SampleTable(Name, Age) VALUES('" & NameFromTXT & "'," & CStr(AgeFromTXT) & ")"
    cmd.Execute
  Loop
  Close #1
  Set cmd = Nothing
  cn.Close
  Set cn = Nothing
7. Start the project and click on the button. After that open SampleDatabase and you will see inserted data into SampleTable.

Plamen Ivanov
MCSD .NET Early Achiever and MCAD .NET Charter Member (VB .NET/SQL Server 2000)
MCSD (VB 6.0/SQL Server 2000)

VB (.NET) - what other language do you need in the whole Universe?...

Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform