Question : I need to read a text file and map it to an Access Table

I have a text file (contactdata.txt) that looks like this:

FirstName = First
LastName = Last
SocialSecurityNumber = 123-45-6789
Address = 100 N Main Street
City = Your City
State = State
Zip = 12345-1234
submit_by = [email protected]
HomePhone = 800-555-1212
CellPhone = 123-555-1212
(not the end of the file, the file continues)

What I need to accomplish is to create a vba script that when a command button is clicked the script will read the text file from it's location (C:\TextFiles\contactdata.txt) and append the data to the proper field in an existing Access table named contacts.

Your direction in helping me accomplish this would be much appreciated.

Answer : I need to read a text file and map it to an Access Table

Hi there, you can try this script, which is VBS code.  You can change the location of the DB and the text file with strDBPath and strTextFile.

Regards,

Rob.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
Set objConn = CreateObject("ADODB.Connection")
strDBPath = "C:\Temp\Users.accdb"
strTextFile = "C:\Temp\Users.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
intForReading = 1
If objFSO.FileExists(strDBPath) = True Then
	strMDBPath = objFSO.GetFile(strDBPath).ShortPath
	objConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDBPath & ";Persist Security Info=False;"
	'objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
	'objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";Jet OLEDB:Database Password=password;"
	'objConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & strDBPath & ";"
	Set objFile = objFSO.OpenTextFile(strTextFile, intForReading, False)
	strFields = ""
	strValues = ""
	While Not objFile.AtEndOfStream
		strLine = Trim(objFile.ReadLine)
		If strLine <> "" Then
			If InStr(strLine, "=") > 0 Then
				strFieldName = Trim(Left(strLine, InStr(strLine, "=") - 1))
				strValue = Trim(Mid(strLine, InStr(strLine, "=") + 1))
				If strFields = "" Then
					strFields = "[" & strFieldName & "]"
				Else
					strFields = strFields & ",[" & strFieldName & "]"
				End If
				If strValues = "" Then
					strValues = "'" & strValue & "'"
				Else
					strValues = strValues & ",'" & strValue & "'"
				End If
			End If
		End If
	Wend
	objFile.Close
	strQuery = "INSERT INTO Contacts(" & strFields & ") VALUES (" & strValues & ");"
	On Error Resume Next
	objConn.Execute(strQuery)
	If Err.Number <> 0 Then
		MsgBox "Error inserting data with query: " & VbCrLf & strQuery & VbCrLf & "Error " & Err.Number & ": " & Err.Description
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0
		MsgBox "Data inserted successfully."
	End If
	objConn.close
	Set objConn = Nothing
Else
	MsgBox "Unable to find " & strDBPath
End If
Random Solutions  
 
programming4us programming4us