OLEDB connection avoid auto convert on text to int - vbscript

when run a simple vbs code to read a csv.
Example CSV:
AB,CD
XYZABDER,TLK431
..
with following code
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
strPathtoTextFile = "C:\xyz\"
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""HDR=YES;FMT=Delimited"""
objRecordset.Open "SELECT * FROM myfile.csv", objConnection
Do Until objRecordset.EOF
MsgBox "AB: " & objRecordset.Fields.Item("AB")
MsgBox "CD: " & objRecordset.Fields.Item("CD")
objRecordset.MoveNext
Loop
it alsways converts "TLK431" to 431. As soon as i change the "TLK" to "TLB" or whatever its read as string "TLB431" for "TL" or "TLK" its converting to int 431.
Cant understand the reason for this auto conversion.
For now my workaround is a schema file. But i doesnt like it and want to ask for a better solution and the reason for this conversion.
Thx

This may be an encoding issue. If the CSV file contains Unicode characters, ensure that both the CSV and VBS files are saved as Unicode (UTF-16), otherwise, save both files as ANSI. Do not use UTF-8.

Related

Selected columns without duplicates using vbs

It’s really appreciated if someone would have help me on this using vbs script.
I have text file with 4 gb data (below sample data--at end it is) and I need only these three columns data out of my attached textfile
"20150901162037","20150901162037","LDAP_Prod/xyrep, tett"
Without duplicate data since I have many of them are similar ldap ids are there.
so i need to take only with unique of ids with three columns,Please can you help me how to achive using vbs script.
Finally I need my output will be following way with out dulicates:
"20150901162037","20150901162037","LDAP_Prod/xyrep, tett"
"20150901162037","20150901162037","LDAP_Prod/ttin_tess"
.
…
my sample data is here:
"100aEB","20150901162037","20150901162037","LDAP_Prod/xyrep, tett","N","11937.91666666667","0","Test_Plan","360610","BAU","Version1","Budget","LDDRR","31029","21240","36542_31029","2016","Jan","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ xyrep, tett ","N","11937.91666666667","0","SS_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Feb","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttin_tess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ xyrep, tett ","N","11937.91666666667","0","SS_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Feb","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttin_tess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttin_tess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttenetess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
"100aEB","20150901162037","20150901162037","LDAP_Prod/ttin_tess","N","11937.91666666667","0","LLL_Plan","360610","BAU","Version1","Budget","LDGR","31029","21240","36542_31029","2016","Aug","6050210000",""
Describe your input file using a schema.ini file:
[33851749.txt]
Format=CSVDelimited
ColNameHeader=False
DecimalSymbol=.
Col1=AA Text
Col2=BB Text
Col3=CC Text
Col4=DD Text
(minimalistic/need to know version to get it 'to work'; you surely can do better)
Open a ADODB Connection to your input file, select your 3 columns 'distinct'ly, and write the records quoted to StdOut (or a file):
Option Explicit
Const adClipString = 2
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim oDB : Set oDB = CreateObject("ADODB.Connection")
Dim sCS : sCS = Join(Array( _
"Provider=Microsoft.Jet.OLEDB.4.0" _
, "Data Source=" & oFS.GetAbsolutePathName("..\data") _
, "Extended Properties=""text""" _
), ";")
oDB.Open sCS
Dim sSQL : sSQL = "Select Distinct BB, CC, DD from [33851749.txt]"
Dim oRS : Set oRS = oDb.Execute(sSQL)
Do Until oRS.EOF
WScript.Echo """" & oRS.GetString(adClipString, 1, """,""", """", """""")
Loop
oDB.Close
That should give you:
cscript 33851749.vbs
"20150901162037","20150901162037","LDAP_Prod/ xyrep, tett"
"20150901162037","20150901162037","LDAP_Prod/ttenetess"
"20150901162037","20150901162037","LDAP_Prod/ttin_tess"
"20150901162037","20150901162037","LDAP_Prod/xyrep, tett"

Text files handles differently

I am trying to read from a csv.txt file using Ado Recordset
I get no results back when trying..
When I copy the contents of the original file into a new text file, and read from that file, it works just fine.
Any ideas what the reason for this might be?
The second file is smaller in size, about 1/2. That's the only difference I can see. This is driving me mad :-)
'Edit
Update with code & schema.ini
Code:
Sub ImportTextFiles()
Dim objAdoDbConnection As ADODB.Connection
Dim objAdoDbRecordset As ADODB.Recordset
Dim strAdodbConnection As String
Dim pathSource As String
Dim filename As String
pathSource = "C:\Users\me\Desktop\Reports\"
filename = "test1.txt"
'filename = "test2.txt"
strAdodbConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & pathSource _
& ";Extended Properties=""text;HDR=yes;FMT=Delimited"";"
Set objAdoDbConnection = CreateObject("Adodb.Connection")
Set objAdoDbRecordset = CreateObject("ADODB.Recordset")
With objAdoDbConnection
.Open (strAdodbConnection)
With objAdoDbRecordset
.Open "Select top 10 * FROM " & filename & " WHERE [Date] > #01/01/2000# ", objAdoDbConnection, adOpenStatic, adLockOptimistic, adCmdText
If Not objAdoDbRecordset.EOF Then objAdoDbRecordset.MoveFirst
Do While Not objAdoDbRecordset.EOF
Debug.Print "Field(0): " & objAdoDbRecordset(0).Value
objAdoDbRecordset.MoveNext
Loop
.Close
End With
.Close
End With
Set objAdoDbRecordset = Nothing
Set objAdoDbConnection = Nothing
End Sub
Schema.ini:
[Test1.txt]
col1=date text
col2=interval integer
col3=application text
[Test2.txt]
col1=date text
col2=interval integer
col3=application text
notepadd++ gave me the answer, file1 is ucs-2 encoded, the newly created utf-8

How to update a recordset

here is my code
Dim Cn1 As ADODB.Connection
Dim iSQLStr As String
Dim field_num As Integer
Set Cn1 = New ADODB.Connection
Cn1.ConnectionString = _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"DefaultDir=" & "C:\path\"
Cn1.Open
iSQLStr = "Select * FROM " & "file.txt" ' & " ORDER BY " & txtField.Text
field_num = CInt(1) - 1
Set Rs1 = Cn1.Execute(iSQLStr)
lstResults.Clear
While Not Rs1.EOF
DoEvents
Rs1.Fields(field_num).Value = "qaz"
If IsNull(Rs1.Fields(field_num).Value) Then
lstResults.AddItem "<null>"
Else
lstResults.AddItem Rs1.Fields(field_num).Value
End If
Rs1.MoveNext
Wend
The error i get is in this line
Rs1.Fields(field_num).Value = "qaz"
it says "The current recordset does not support updating", what is wrong in the code?
I'm not sure if this is valid for text files but with SQL Server you need to change the LockTypeEnum Value setting to allow editing see this link, the default is adLockReadOnly
Edit
According to this link it is not possible to edit a text file via ADO.

Lookup function in VB6

I have a table in an excel file called " Employee_States_File".This table contains two columns Name and States. Both columns are filled with data. The province table contains abbreviation of the States such as " NE, WA" and so. I have another excel file called " States_File" which contains a table that has two columns: Abbreviation and FullStateName. This table is considered to be a lookup table to look for the full state names based on the abbreviation. Now, I want to write a code in VB6 so that when the user click a button, All the abbreviation names in the table of the excel file " Employee_States" are changes into the full state names based on the lookup table of the excel sheet " States_File".
does it make sense?
Please help,
If you are allowed to convert them to csv files (comma delimited text files) you can use the Jet Database engine and do normal SQL joins.
I use this to setup the connection.
Public Function OpenTextConnection(ByVal FileName As String) As Connection
Dim FSO As FileSystemObject
Dim DBFolder As String
Dim TS As TextStream
Set FSO = New FileSystemObject
DBFolder = FSO.GetParentFolderName(FileName)
If FSO.FileExists(FSO.BuildPath(DBFolder, "Schema.ini")) Then
FSO.DeleteFile (FSO.BuildPath(DBFolder, "Schema.ini"))
End If
Set TS = FSO.CreateTextFile(FSO.BuildPath(DBFolder, "Schema.ini"))
TS.WriteLine "[" & FSO.GetFileName(FileName) & "]"
TS.WriteLine "Format=CSVDelimited"
TS.WriteLine "ColNameHeader = True"
TS.WriteLine "MaxScanRows = 0"
TS.Close
Set OpenTextConnection = New Connection
If FSO.FolderExists(DBFolder) Then
OpenTextConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBFolder & ";Extended Properties=""text;HDR=Yes;FMT=Delimited;"";"
Else
MsgBox DBFolder & " Does not exists.", vbExclamation
End If
End Function
Each file is a table in the connection and you can do SQL joins. Note the example is just a simply open up a table. You can use any valid SQL Syntax.
Dim DB1 As Connection
Dim TB As Recordset
Dim FSO As FileSystemObject
Dim tImport As New DBImportList
Set FSO = New FileSystemObject
Set tImport = New DBImportList
If FSO.FileExists(FileName) Then
Set DB1 = OpenTextConnection(FileName)
Set TB = New Recordset
TB.Open "SELECT * FROM [" & FSO.GetFile(FileName).Name & "]", DB1, adOpenKeyset, adLockOptimistic, adCmdText
End If

VBScript Invalid Character 800A0408 compilation error

I get a compilation error when I try to run the following vbs code from a command prompt in Windows 7.
Option Explicit
Dim objNetwork, strRemotePath1, strRemotePath2, strRemotePath3
Dim strDriveLetter1, strDriveLetter2, strDriveLetter3, strUserName
Set objNetwork = CreateObject("WScript.Network")
strUserName = objNetwork.UserName
strDriveLetter1 = "H:"
strDriveLetter2 = "P:"
strDriveLetter3 = "S:"
strRemotePath1 = "\\test\public\users\" & strUserName & "\"
strRemotePath2 = "\\test\public\groups\"
strRemotePath3 = "\\test\scans\"
'Section which maps two drives, M: and P: and S:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
objNetwork.MapNetworkDrive strDriveLetter3, strRemotePath3
'Extra code just to add a message box
WScript.Echo "Map drives " & strDriveLetter1 & " & " & strDriveLetter2 & " & " & strDriveLetter3
Wscript.Quit
It says the issue is with Line 1 Char 1. Any ideas?
A possible problem is how your file was encoded; try to save it as ANSI and run it again.
FYI for those with the same problem in the future, to fix this:
Open the .vbs in notepad
Go to file and "save as"
Right under the file name box, you will see a drop down menu for encoding. Choose ANSI.
Check you are not using extended charater like á é í ó ú ñ in a variable's name

Resources