I use this function for getting info from an text-file:
Function Import_Kundendaten_FromText(sqlstring As String) As String
'On Error GoTo MeinEnde
Dim cn As ADODB.connection
Dim rec As ADODB.Recordset
Set cn = New ADODB.connection
On Error Resume Next
cn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=" & "" & ";" & "Extensions=asc,csv,tab,txt;"
On Error GoTo 0
If cn.State <> adStateOpen Then Exit Function
On Error Resume Next
Set rec = New ADODB.Recordset
rec.Open LCase(sqlstring), cn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Not rec.BOF And Not rec.EOF Then Import_Kundendaten_FromText = rec.Fields(0).Value '
MeinEnde:
If Err <> 0 Then fehlerverarbeitung ("Err-Nr: " & Err.Number & Chr(10) & "Err-Desc: " & Err.Description & Chr(10) & _
"Err-Source: " & Err.Source & Chr(10) & "Sub Import_Kundendaten_FromText" & Chr(10) & Now())
End Function
If I use this like
Import_Kundendaten_FromText("SELECT SOMETEXTFIELD FROM C:\variable_outl_kunden.txt WHERE CustNo = 105")
=> works perfect!
Import_Kundendaten_FromText("SELECT SOMETEXTFIELD FROM C:\variable_outl_kunden.txt WHERE CustName = 'Someone'")
=> does not give any results nor an error!!!
I have no idea, why I can not use text in the where-staement - any ideas?
Thanks! Max
CodePudding user response:
There is a number of problems with your code.
- As mentioned in the comments, you need a Schema.ini file for your code to work. - I assume you have got one already because otherwise your code would only work if the text file matches the default settings of the Text Driver.
- The
On Error Resume Nextstatements, particularly the second one, in your code are most likely the reason why you do not see any error message. - The column names and data in the text file will be treated case sensitive. So your
LCase(sqlstring)is not a good idea unless the text file content is all lower case.
