I'm having trouble in reading csv file that are having alphanumeric data. below is my code in classic asp:
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
ls_map_path & ";Extended Properties=""Text;HDR=Yes;FMT=Delimited"";"
Set lo_conn = Server.CreateObject("ADODB.Connection")
lo_conn.Open strConn
Set lo_rs = Server.CreateObject("ADODB.recordset")
lo_rs.CursorLocation = adUseClient
lo_rs.open "SELECT * FROM " & as_file_name, lo_conn, adOpenStatic, adLockOptimistic, adCmdText
and below is the data:
user_id,status,last_name,first_name,middle_name
1234,1,DeVera,athan,M.
1234,1,De Vera,athan,M.
ABC1,1,Santos,Shaine
abcd,1,Santos,Luis
1234,1,De Vera,athan,M.
1234,1,De Vera,athan,M.
ABC1,1,Santos,Shaine
When reading "user_id" column using lo_rs.fields.Item("user_id"), it perfectly retrieve the "1234" user_id value. but other data that are having alphanumeric value is returning me a null.
I don't know the reason why it is returning null. Though, if the data is all alphanumeric then it perfectly reads the user_id column. I think the only problem is, if the csv data is having a mix numeric and alphanumeric value in one column.
Does anyone know how to resolve this? or maybe I just have a missing text in the connection string.
Please advise and thank you very much for the help in advance!
To get around the type inference you can create a SCHEMA.INI that defines the types of each column in the CSV file.
Set HDR=NO & in the directory containing the CSV (ls_map_path) create a schema.ini:
[filenamehere.csv]
Col1=user_id Text
Col2=status Long
Col3=last_name Text
Col4=middle_name Text
The type mappings used by the text provider are now based on the above schema.
Related
On screen UI, use unbound field to enter mix of ASCII and Unicode character text string for searching. On the screen, it is correct - for example "White白色". But, on VBA code, the Unicode character of the unbound filed becomes to '?' and cannot be used for searching - "White??" for the above example. How to get the mixed ASCII and Unicode string as on the screen for VBA code?
Below is my code. Me.txName is unbound text field, fiterstr is subform filter. It works, if name is all ASCII. It will search '?', if Unicode is entered.
Dim filterstr As String
If Me.txName <> "" Then
filterstr = "(Customer.Company LIKE '*" & Me.txName & "*')"
End If
Me.sfmCustomerList4Search.Form.Filter = filterstr
With Erik A. help, this question has been solved. On my question, there are two problems.
Access Msgbox does not support unicode. Erik A. has written up an unicode-compatible messagebox-implementation MsgboxW here
Data is stored ad SQL Server while Access is front end UI. SQL collation must be setup to compatible with Unicode language. Please see See Chinese collation for MS SQL.
Problem Description:
I am using Microsoft Access Plus 2010, with below code to export the result of query to Word table. However, there are all kinds of special characters exported if the record is over 255 characters.
Below are the query, VBA etc.
Query name: Qa
Query function: select field from Ta
VBA:
Dim qbf As QueryDef
Dim dabase As Database
Set dabase = CurrentDb
Set qdf = dbase.QueryDefs(Qa)
Dim results As Recordset
Dim flds As String
Set results = qdf.OpenRecordset()
While (Not results.EOF)
doc.addRecord results
results.MoveNext
Wend
qdf.Close
Public Sub addRecord(pubRecordSet As Recordset)
flds = pubRecordSet.Fields("fieldname")
mTable.cell(1, 1).range.InsertAfter (flds)
...
End Sub
Where 'mTable' is a Word table object, 'fieldname' is the name of the field to be exported to Word Table.
This VBA in general works fine when the length of flds is less than 255, however, it throws a lot of special characters in the Table cell if the length exceeds 255.
Example on special characters exported to Word table cell:
退D瞻껙皿 Ƭ" " ᬈ௩Hȷ⫗ 鋨Dィ௨瞻껥皿௲Ǭ" "Tೕ ŮԱ ซ鐌D
I checked the limitation of MS Access from link here. It mentions the recordset of query is 1GB, which my data is way less (~255 characters). Any help is appreciated.
I think they are being truncated or corrupted almost certainly to do with one of the reasons listed here : http://allenbrowne.com/ser-63.html
By definition if they are over 255 character long they will be interpreted as Memo or Long text (Same definition - Memo is the older name of the data type).
Not sure what I'm missing. I'm trying to loop through all fields in a table and if any data type is <> text, then change it to text. We have some fields coming in as a number but need to convert to text before exporting as a .txt. I cannot add quotations to the field - each field must only contain numbers and letters.
I have tried the following and used the db.execute alter line for each field, but I am receiving a run time 3047 Record Too large syntax error after about the 6th field. I'm assuming it would be best to somehow loop to check each field and only change to text if it isn't already?
Dim table As DAO.TableDef
Dim db As DAO.Database
Set db = CurrentDb
db.Execute "ALTER TABLE ImportFromExcel " _
& "ALTER COLUMN RPT_SPLIT_ID CHAR;"
The easiest method is to cast the field as a string in your SELECT statement. Use the CStr() function to cast whatever non-text field you have to text. When possible, always try to collect your data with a healthy SQL query rather than looping through datasets. It may not be a noticeable difference in smaller tables, but it makes a huge difference with large record sets. Try this:
SELECT CStr(field1), CStr(field2), CStr(field3)
FROM ImportFromExcel
I have a lot of databases I would like to change their column names. These databases were designed by a team which used Portuguese words for column names. I have managed to change names with spaces but when I try to change the names for columns with Portuguese accents e.g Instalaçao, my VBScript fails with error item not found in this collection. My VBScript is for changing this column is as below.
tblName = "CONSUMIDORES"
oldColName = "[Instalaçao]"
newColName = "INSTALACAO"
Set dbe = CreateObject("DAO.DBEngine.120")
Set db = dbe.OpenDatabase(dbPath)
Set fld = db.TableDefs(tblName).Fields(oldColName)
fld.Name = newColName
This code works for other columns with spaces but for accented words it fails. I am using MS Access 2013. I am new to VBScript.
Converting the file to ANSI as suggested by Gord Thompson worked.
I'd try to get away with refering to the fields by number:
Set fld = db.TableDefs(tblName).Fields(14)
(assuming Instalaçao is the 15th field of that table).
I have an Access database that must connect to Oracle programmatically to create a linked table. The connection string is of the form:
ODBC;Driver={Microsoft ODBC for Oracle};Pwd=<Password>;UID=<User>;Server=<Server>
Currently the login info is hardcoded.
I now have to have the tool connect to different databases. I was going to simply let the user enter the <User>, <Password>, and <Server> and then just concatenate it all together into a single connection string. I'm pretty sure this is SQL Injection safe because the connection doesn't actually exist at this point, but I'm not 100% certain - is this a valid concernt, and if so how would I sanitize these inputs (which come from free-form text fields)?
This is not called SQL Injection because the connection string doesn't allow execution of arbitrary SQL code.
If you are giving users access to the database from the desktop then SQL Injection probably isn't a very relevant concern anyway. Why would anyone bother trying to inject SQL through an application vulnerability when it's much easier for him just to create a connection himself using his valid credentials?
It appears that your concern is valid, as evidenced by the fact that ADO.NET has a set of Connection String Builder classes (though it's more accurate to call it "connection string injection" vs. "SQL injection" since there's no SQL involved). Since you're not using .NET, the next best option is input sanitization and escaping special characters. The MSDN reference on OLEDB connection string syntax states that:
To include values that contain a
semicolon, single-quote character, or
double-quote character, the value must
be enclosed in double quotes.
and
If the value contains both
single-quote and double-quote
characters, the quote character used
to enclose the value must be doubled
each time it occurs within the value.
This is a VBScript I put together which attempts to implement the two guidelines above:
Option Explicit
Dim pw, connStr, conn
pw = InputBox("Enter password")
' Sanitize double quotes in the input string
pw = Replace(pw, Chr(34), Chr(34) & Chr(34))
' Notice how pw is surrounded by double quote characters
connStr = "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;User ID=test_user;Password=" & Chr(34) & pw & Chr(34)
' Test the connection. We'll get a runtime error if it didn't work
Set conn = CreateObject("ADODB.Connection")
conn.Open connStr
conn.Close
WScript.Echo "OK!"
If my password were app"le'\, the connection string would end up as:
Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;User ID=test_user;Password="app""le'\"
However, this doesn't work for all possible inputs. For example, the test script gives an error when the password contains a double quote before a semicolon. It could be that I'm interpreting the guidelines incorrectly. I'm not sure, but hopefully, this at least gets you started.