VBScript to `drop` and `create` new table in microsoft access - vbscript

I have VBScript to drop and create new table in microsoft access.
My vb script is :
Set dbeng = CreateObject("DAO.DBEngine.120")
strMdbFile = "amw\db_amw.accdb"
Set db = dbeng.OpenDatabase(strMdbFile)
strSql1 = "DROP TABLE amw"
StrSql2 = "SELECT * INTO amw FROM MPN_V WHERE (((Format([tgl_bayar],'yyyy')) Between Format(Now(),'yyyy')-2 And Format(Now(),'yyyy')))"
strSql3 = "DROP TABLE UPDATE"
strSql4 = "SELECT * INTO update FROM UPDATE6 WHERE id="1""
db.Execute(strSql1)
db.Execute(strSql2)
db.Execute(strSql3)
db.Execute(strSql4)
when the script runs the line db.Execute(strSql1) and db.Execute(strSql2), no error Happen. but, when the script runs the line db.Execute(strSql3) and db.Execute(strSql4), error Happen : Expected end of statement, code 800A0401, source Microsoft VBScript compilation error. Please correct my Script? thank you verymuch

The problem is with the number of quotes on the last SQL statement.
strSql4 = "SELECT * INTO [update] FROM UPDATE6 WHERE id="1""
If "id" is an integer, use the following:
strSql4 = "SELECT * INTO [update] FROM UPDATE6 WHERE id=1"
If "id" is a string, use the following:
strSql4 = "SELECT * INTO [update] FROM UPDATE6 WHERE id=""1"""
Doubling the quotes are necessary when working with a string within a string.

Related

How to get MST properties from vbscript

So, I am creating a vbscript that will read an MSI and MST file. The idea is that if the user that will run the script is testing an MSI with an MST file involved, the script should create a "report" of the new properties that this MST have.
I am able to get the properties from a regular MSI, the problem is when I am trying to get into the MST section. While doing research I found out about the _TransformView Table and this should help me to obtain this information but I think I am not sure I know how to handle that table.
Const msiTransformErrorViewTransform = 256
Const msiOpenDB = 2
Dim FS, TS, WI, DB, View, Rec
Set WI = CreateObject("WindowsInstaller.Installer")
Set DB = WI.OpenDatabase(msiPath,msiOpenDB)
DB.ApplyTransform mstPath, msiTransformErrorViewTransform
If Err.number Then
Exit Function
End If
For i = 0 To 24 'Number of properties on the arrPropertyList
Set View = DB.OpenView("Select `Value` From Property WHERE `Property` = " & "'" & arrPropertyList(i) & "'")
View.Execute
Set Rec = View.Fetch
If Not Rec Is Nothing Then
objLog.WriteLine arrPropertyList(i) & " = " & Rec.StringData(1)
End If
Next
That code will display the msi properties that I have added on the arrPropertyList. The thing is that I am looking for the MST properties and I am only getting the MSI ones. I know that I should change the Query to access the _TransformView Table when calling the DB.OpenView but not sure how can I get to this information! Any knowledge you can share would be welcome.
It works slightly differently to what you think. Run the following to see what I mean (maybe force the VBS to run with Cscript.exe from a command prompt if you're expecting a lot of output):
'create 2 constants - one for when we want to just query the MSI (read) and one for when we want to make changes (write)
Const msiOpenDatabaseModeReadOnly = 0
Const msiOpenDatabaseModeTransact = 1
Const msiTransformErrorViewTransform = 256
'create WindowsInstaller.Installer object
Dim oInstaller : Set oInstaller = CreateObject("WindowsInstaller.Installer")
'open the MSI (the first argument supplied to the vbscript)
Dim oDatabase : Set oDatabase = oInstaller.OpenDatabase("C:\Temp\Temp.msi",msiOpenDatabaseModeReadOnly)
oDatabase.ApplyTransform "C:\Temp\Temp.mst", msiTransformErrorViewTransform
'create a view of the registry we want to see
Dim sql : sql = "SELECT * FROM `_TransformView`"
Dim regView : Set regView = oDatabase.OpenView(sql)
'execute the query
regView.Execute
'fetch the first row of data (if there is one!)
Dim regRecord : Set regRecord = regView.Fetch
'whilst we've returned a row and therefore regRecord is not Nothing
While Not regRecord Is Nothing
'print out the registry key
wscript.echo "Table: " & regRecord.StringData(1)
wscript.echo "Column: " & regRecord.StringData(2)
wscript.echo "Row: " & regRecord.StringData(3)
wscript.echo "Data: " & regRecord.StringData(4)
wscript.echo "Current: " & regRecord.StringData(5)
wscript.echo "***"
'go and fetch the next row of data
Set regRecord = regView.Fetch
Wend
regView.Close
Set regView = Nothing
Set regRecord = Nothing
Set oDatabase = Nothing
Set oInstaller = Nothing
So if you only wanted to see changes in the Property table, you would change the SQL query to:
Dim sql : sql = "SELECT * FROM `_TransformView` WHERE `Table` = 'Property'"
As well as storing the column names of the changed entries, the 'Column' column in the '_TransformView' table also stores whether the value was inserted, removed etc by using the values:
INSERT, DELETE, CREATE, or DROP.
You can find lots of VBScript Windows Installer tutorials for reference - don't forget to set your objects to Nothing otherwise you'll leave handles open. And of course use the link you provided for further reference.
WiLstXfm.vbs: Are you familiar with the MSI SDK sample: wilstxfm.vbs (View a Transform)? It can be used to view transform files. Usage is as follows:
cscript.exe WiLstXfm.vbs MySetup.msi MySetup.mst
Mock-up output:
Property Value [INSTALLLEVEL] {100}->{102}
File DELETE [Help.chm]
I think all you need is in there? Maybe give it a quick look. There is a whole bunch of such MSI API Samples - for all kinds of MSI purposes.
Github.com / Windows SDK: These VBScripts are installed with the Windows SDK, so you can find them on your local disk if you have Visual Studio installed, but you can also find them on Github.com:
Github: WiLstXfm.vbs - Microsoft repository on github.com.
Disk: On your local disk, search under Program Files (x86) if you have Visual Studio installed. Current Example: %ProgramFiles(x86)%\Windows Kits\10\bin\10.0.17763.0\x86.

ADODB.Command: Prepared statement not returning expected number of records

I'm using ADODB.Command to execute prepared statements in Classic ASP, however the number of records being return by MariaDB is incorrect, and I can't figure out why.
The code below gives an example of the problem I'm having:
<%
Dim conn, comm, rs, SQL, SQLparam
set conn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.Recordset")
conn.Open("DSN=localhost")
' Standard execution
SQL = "SELECT * FROM tests WHERE ID > 0"
set rs = conn.Execute(SQL)
response.write "<p>Results: " & uBound(rs.getRows(),2) & "</p>"
' Prepared statement execution
SQL = "SELECT * FROM tests WHERE ID > ?"
SQLparam = 0
Set comm = Server.CreateObject("ADODB.Command")
comm.ActiveConnection = conn
comm.CommandText = SQL
comm.Parameters.Append(comm.CreateParameter("#param",varType(SQLparam),1,250,SQLparam))
set rs = comm.Execute()
response.write "<p>Results: " & uBound(rs.getRows(),2) & "</p>"
set comm = nothing
rs.close() : set rs = nothing
conn.close() : set conn = nothing
%>
Output:
Results: 4433 ' as expected
Results: 10
I feel like I must be missing something obvious. I've played around with the "CreateParameter" settings as outlined by Microsoft here, but the wrong number of records are always returned by MariaDB, never mind what I change.
What's also strange is that if I change the SQL to do a SELECT COUNT, the prepared statement returns the correct count value, it just doesn't return the correct number of rows when I try to select them.
After some further testing this appears to be a driver issue. I'm using MariaDB but with the MySQL ODBC connector (8.0 Unicode Driver). After switching to the MariaDB ODBC connector the prepared statement works as expected. I apologise for not testing further before posting.
I guess MariaDB and the MySQL ODBC connector are incompatible when trying to perform certain database functionality, because up until now I've never had any issues.

[Microsoft][ODBC driver for Oracle][Oracle]ORA-00911 in VBScript

When I use the query directly it's works, but when I run in it with VBscript this error occurs.
The connection with de DB works.
global_OracleConn = Createobject("ADODB.Connection")
global_OracleConn.Open connectionString
query = "UPDATE DB.TabelX SET X_DT_ = SYSDATE + 360, "_
&"X_Amount_MAX_ID = 100, X_Amount_IN_REQUEST = '1', X_NUM = 15000,"_
&"X_VALUE_LIMIT = 15000, SCORE = 0,
&"WHERE ROW_ID IN (SELECT X_ULTIMA_ID FROM DB.TabelY "_
&"WHERE OU_NUM IN ('"&varID&"'));"
global_OracleConn.Execute(Query)
I tried use others breaklines or put all in one line, but the error still occurs.
There are several issues:
Remove the semicolon at the end from your SQL string
The quoting is wrong. You missed a double quote after SCORE = 0,
Ancient Microsoft ODBC Driver for Oracle is deprecated for ages. Use the ODBC driver from Oracle
Use prepared statement with bind parameters, i.e.

run time error '13' type mismatch

I have my query on VB6 which was:
Set Db = DBEngine.OpenDatabase(App.Path & "\sample4nC4.mdb")
Set rs = Db.OpenRecordset("select *from tbl_student;")
Do Until rs.EOF
With ListView1
.ListItems.Add , , rs.Fields("stud_ID")
.ListItems(ListView.ListItems.Count).SubItems(1) = rs.Fields("stud_fname")
.ListItems(ListView1.ListItems.Count).SubItems(2) = rs.Fields("stud_lname")
.ListItems(ListView1.ListItems.Count).SubItems(3) = rs.Fields("stud_address")
.ListItems(ListView1.ListItems.Count).SubItems(4) = rs.Fields("stud_age")
End With
rs.MoveNext
Loop
When I execute this query, there was an error on line 2 says:
Run Time Error '13' Type Mismatch
I really don't get it because when I check the table name, it was correct and yet I cant access the table. Can anybody answer my problem?
Do you have references to ADO and DAO in your project?
If so, look at this Microsoft support article: https://support.microsoft.com/en-us/kb/181542
Do these
Replace your query from 'select *from tbl_student;'
to
'select stud_fname, stud_lname, stud_address, stud_age from tbl_student'
This includes (1. space between '' and 'from' 2. remove ';' 3. specify the field names explicitly)*
Put a breakpoint in the first line your program and step into, if it still fails check which line it's failing.

Update function on a RecordSet object in VBscript causing DBISAM parse error

I'm having difficulty using the Update function on a RecordSet object while using the DBISAM 4 ODBC driver. Here is what my code looks like.
dtmNewDate = DateSerial(1997, 2, 3)
MsgBox(dtmNewDate)
'Create connection object & connection string
Set AConnection = CreateObject("ADODB.Connection")
strConnection = "Driver={DBISAM 4 ODBC Driver}; CatalogName=S:\RAPID\Z998\2008; ReadOnly=False"
Aconnection.Mode = adModeReadWrite
AConnection.Open strConnection
'create SQL statement to be run in order to populate the recordset
strSQLEmployeeBDate = "SELECT * FROM Z998EMPL WHERE state = 'NY'"
'Create Recordset object
Set rsRecSet = CreateObject("ADODB.Recordset")
rsRecSet.LockType = 2
rsRecSet.Open strSQLEmployeeBDate, AConnection
While Not rsRecSet.EOF
rsRecSet.Fields("BIRTHDATE").value = dtmNewDate
rsRecSet.Update
rsRecSet.MoveNext
Wend
When I try to execute this code I receive the following error:
"DBISAM Engine Error #11949 SQL Parsing error- Expected ( but instead found = in UPDATE SQL statement at line 1, column 336"
I can't figure out what is causing this error. Does anyone have any ideas as to what is causing it?
DBISAM error messages Check the field name.

Resources