Syntax Error in From clause - VB6 - vb6

Having a problem getting the above Error Message.
Can anyone assist please ?
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim RS As New ADODB.Recordset
Dim RS2 As New ADODB.Recordset
Set cn = New ADODB.Connection
Call cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & TheServer &
FileTypeOld & ";" & "Jet OLEDB:Database Password=12345678;")
If cn.State = 0 Then cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "SELECT * FROM mov"
cmd.CommandType = adCmdTable
Set RS = cmd.Execute

The problem is you are using an SQL statement with a Command type adCmdTable. When using that command type, ADO will generate the select query internally, you are expected in this case to just specify the name of the table.
Alternatively, just remove the cmd.CommandType = adCmdTable line all together and keep the select * syntax.

You need ton add this character _ which is underscore at the end of this line
Just replace with this:
Call cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & TheServer & _
FileTypeOld & ";" & "Jet OLEDB:Database Password=12345678;")
or you can merge both lines into one line without underscore, just like this
Call cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & TheServer & ileTypeOld & ";" & "Jet OLEDB:Database Password=12345678;")

Related

How can I Detect an ADO Connection Error? [duplicate]

When a SQL batch returns more than one message from e.g. print statements, then I can only retrieve the first one using the ADO connection's Errors collection. How do I get the rest of the messages?
If I run this script:
Option Explicit
Dim conn
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "SQLOLEDB"
conn.ConnectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=Master"
conn.Open
conn.Execute("print 'Foo'" & vbCrLf & "print 'Bar'" & vbCrLf & "raiserror ('xyz', 10, 127)")
Dim error
For Each error in conn.Errors
MsgBox error.Description
Next
Then I only get "Foo" back, never "Bar" or "xyz".
Is there a way to get the remaining messages?
I figured it out on my own.
This works:
Option Explicit
Dim conn
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "SQLOLEDB"
conn.ConnectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=Master"
conn.Open
Dim rs
Set rs = conn.Execute("print 'Foo'" & vbCrLf & "print 'Bar'" & vbCrLf & "raiserror ('xyz', 10, 127)")
Dim error
While not (rs is nothing)
For Each error in conn.Errors
MsgBox error.Description
Next
Set rs = rs.NextRecordSet
Wend

Getting Run-time error '3706' error while connecting to Oracle DB using VBA in excel

I am trying to connect to Oracle DB for the first time using the ODBC Datasource Connector(named Demo_DSN) When I Step Into (F8) the code is giving Run-time error '3706': application-defined or object -defined error
Sub ConnectToOracle()
Dim cn As ADODB.Connection
Dim rs As ADODB.RecordSet
Dim mtxdata As Variant
Set cn = New ADODB.Connnection
Set rs = New ADODB.Recordset
cn.Open ( _
"User ID = XXGMDMADM" & _
";Password=xxgmdmadmdb" & _
";Data Source= Demo_DSN" &_
";Provider= OraOLEDB.Oracle")
'Cleanup in the end
Set rs = Nothing
Set cn = Nothing
End Sub

vbscript: how to query csv with ado and return csv

I am trying to make this script work (having some vba knowledge and no idea about vbs differences. The solution I am trying to adapt is here:
VB Script to dump an SQL Server table to CSV and
https://msdn.microsoft.com/en-us/library/ms974559.aspx
It is supposed to read csv file, run ACE SQL query on it and return the result to another CSV
It generates only an empty Output.csv file and it is locked for editing. Can you help me out:
On Error Resume Next
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strPathtoTextFile = "C:\Databases\"
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
rs.Open "SELECT * FROM PhoneList.csv", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
Set fso=CreateObject("Scripting.FileSystemObject")
Set ts=fso.OpenTextFile("c:\Databases\output.csv",2,TRUE)
line=""
For Each tmp In objRecordset.Fields
line=line & tmp.Name & ","
Next
ts.WriteLine Left(line,Len(line)-1)
While Not rs.EOF
line=""
For Each tmp In rs.Fields
If IsNull(tmp.Value) Then
line=line & """" & Replace(tmp.Value,"""","""""") & ""","
Else
line=line & """" & tmp.Value & ""","
End If
Next
ts.WriteLine Left(line,Len(line)-1)
rs.MoveNext
Wend
Set rs = Nothing
ts.close
rs.close
fso.close
cn.Close
I understand some VBA but am not able to find my way out in this VBS. Can you help me find out what I am doing wrong?
Secondly... is it possible to make all the paths to data relative to the script itself, so that the solution can be distributed? I know how to do it in vba but have no idea if it is possible with vbscript?
I am sure such a template can be very helpful to all the community. If my question is ill formulated I will humbly accept the critique.
I'm adding the working version of the code here. It should be helpful for you to remove the "On Error Resume Next" and try to fix the errors that are highlighted. There are not many, they are not difficult to fix but the exercise should be informative. Here's the working version of the code:
On Error Resume Next
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strPathtoTextFile = "C:\Databases\"
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
rs.Open "SELECT * FROM PhoneList.csv", _
cn, adOpenStatic, adLockOptimistic, adCmdText
Set fso=CreateObject("Scripting.FileSystemObject")
Set ts=fso.OpenTextFile(strPathtoTextFile & "output.csv",2,TRUE)
line=""
For Each tmp In rs.Fields
line=line & tmp.Name & ","
Next
ts.WriteLine Left(line,Len(line)-1)
While Not rs.EOF
line=""
For Each tmp In rs.Fields
If IsNull(tmp.Value) Then
line=line & """" & Replace(tmp.Value,"""","""""") & ""","
Else
line=line & """" & tmp.Value & ""","
End If
Next
ts.WriteLine Left(line,Len(line)-1)
rs.MoveNext
Wend
rs.close
Set rs = Nothing
ts.close
cn.Close

Error in connecting with ms access database with password using VB6

Private Sub Form_Load()
Dim conString As String
conString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=D:\Dheeraj\VB6_DH\db5.mdb" _
& "Jet OLEDB:Database Password=dheeraj;" _
'& "App.Path & Persist Security Info=False;"
Set CON = New ADODB.Connection
With CON
.ConnectionString = conString
.Open
End With
End Sub
Hi,
here is the code to connect ms access database with password protection. However it is giving an error 'Could not use ";file already in use' can you please tell me what could be the issue.
Try to do this.
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Masterfile.mdb;Jet OLEDB:Database Password=xxxxx;"
Missing ; between .mdb and Jet
conString = "Provider=Microsoft.Jet.OLEDB.4.0" _
& ";Data Source=D:\Dheeraj\VB6_DH\db5.mdb" _
& ";Jet OLEDB:Database Password=dheeraj;"
This is a connection string I just pulled from on of my (working) project:
"Provider='Microsoft.Jet.OLEDB.4.0';Data Source='%path%\%file%'; Jet OLEDB:Database Password=%pwd%;"
So I guess the database source should be enclosed in a pair of single-quotes.
EDIT: Cross that, it shouldn't make any difference (as with the single quotes around the provider name). I never read your question until the end.
Close any programs currently having the database open and then delete any *.ldb file left in the same directory as the database. If you cannot delete the *.ldb file, then it means there's still a process running that has that file open. Hunt it down and kill it, then retry deleting the file.
You need to add two ; before Jet OLEDB
And try this code for connection
Public Con as New ADODB.Connection
Private Sub Form_Load()
Dim conString As String
conString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=D:\Dheeraj\VB6_DH\db5.mdb" _
& ";;Jet OLEDB:Database Password=dheeraj;"
Con.Open conString
End Sub
Dim Con As ADODB.Connection
Dim DatabasePath As String
Dim DatabasePassword As String
DatabasePath = App.Path & "\Storage.mdb"
DatabasePassword = "mypc"
Set Con = New ADODB.Connection
With Con
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';" & _
"User ID=Admin;Data Source=" & DatabasePath & ";" & _
"Jet OLEDB:Database Password='" & DatabasePassword & "'"
.Open
End With

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.

Resources