Access VBADiscuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
I want a sample code, which can read data from txt file and store it in a table , actually my requirement is to count lines of code i have written in a txt file ( total lines of code - comments lines (* at seventh column )
Can you post a couple lines from the text file? This will help in knowing how to capture the data. Then post what the table will look like that you want to store the data in, WITH some sample data. For example, if your file is:
SampleTextFile001.txt
- line of code
- line of code
- //comment line
- line of code
And your table is:
tblCodeFile
CodeFileID - autonumber PK
FileNameAndPath - text
LinesOfCode - number
You can do this without storing every single line in the table. The code counter can be made to do this arithmetic for you. From this sample, it could return and store 19 (22-3=19). Do you want it to store this value? Or do you want it to store every complete line of code, or do you want it to store every line of code at the 7th chraracter, etc? Answer that question and I can post the code.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(sFile) Then
Set objFile = objFSO.OpenTextFile(sFile, 1)
Do Until objFile.AtEndOfStream
sLine = objFile.ReadLine
iComment = InStr(sLine, "*")
If iComment = 0 Then
iCount = iCount + 1
End If
Loop
End If
iCount = 19.
Are you using DAO or ADO to add this to a table? I use ADO so would do this:
sSQL = "SELECT * FROM tblMyTextCounterTable"
Set rs = New ADODB.Recordset
rs.Open sSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic