Multiple query Records Vbscript - vbscript

Pulling multiple records onto one label is not supported in the currently released version of BarTender. So I heard that a VBScript can help me to achieve this. I never touch this before, so sorry for my lack of comprehension
I am trying to create a VBScript that connect to my database and execute the following query :
select Quantite, description_device
from utilise, medical_devices
where utilise.id_operation = 25
and utilise.id_dispositif = medical_devices.id_device
I created this :
Dim cn, rs
set cn = CreateObject("ADODB.Connection")
set rs = CreateObject("ADODB.Recordset")
cn.connectionString = "Driver={MySQL ODBC 5.3 Driver};Server=myip;Database=mydb;User=myuser; Password=mypassword;"
cn.open
rs.open "select ...", cn, 3
rs.MoveFirst
while not rs.eof
wscript.echo rs(0)
rs.next
wend
cn.close
But even the first line fail with this error : <Line 1: Value = Dim cn, rs: Syntax error>
How to proceed to get this script running?

Are you using Bartender Software to create the VBScript?
You have to choose Multi-Line Script under Script Type

Related

How do I find the name of tables in a database? ASP Classic

I'm working on a asp-classic vbscript website and am curious if there is a way to display the tables in a database. I have looked online and saw some examples but they don't explain what to actually do with the code. I am VERY new so if someone could make this make sense for me that would be awesome.
DBMS: Microsoft SQL Server
Set Cat = CreateObject("ADOX.Catalog")
Cat.ActiveConnection = "Provider=sqloledb;Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
For Each Table In Cat.Tables
WScript.Echo Table.Name
Next
To get you started:
Const adSchemaTables = 20 ' 00000014
Dim sCS : sCS = "...your connection string..."
Dim oDb : Set oDb = CreateObject("ADODB.CONNECTION")
oDb.Open sCS
Dim oRs : Set oRs = oDb.OpenSchema(adSchemaTables)
Do Until oRs.EOF
If oRs("TABLE_TYPE") = "TABLE" Then WScript.Echo oRs("TABLE_NAME")
oRs.MoveNext
Loop
[If you really don't know about connectionstrings, look here.

Copy one Recordset to another

Since I received so nice and fast solution to my problem, I will try again to get some help from you:
I opened two Recordsets.
Set cmd1.ActiveConnection = cn1
cmd1.CommandText = "SELECT * FROM mov Where [Date] >= #" & DateA & "#;"
Set RSold = cmd1.Execute
Set cmd2.ActiveConnection = cn2
cmd2.CommandText = "SELECT * FROM mov"
Set RSnew = cmd2.Execute
(I want to save only selected records of a file.)
I know how to copy record by record, but is there a 'Short Cut' to do it faster ?
Thanks
try this:
Dim i As Long
Do While Not RSold.EOF
' You can place if condition here
RSNew.AddNew
For i = 0 To RSold.Fields.Count - 1
RSNew.Fields(RSold.Fields(i).Name) = RSold.Fields(i).Value
Next i
RSNew.Update
RSold.MoveNext
Loop
This will copy records from RSold to RSnew recordset
You Can use code :
Set RSNew = RSOld.Clone
#user1838163 :Saving the second Recordset as a file
Dim RFileNm As String
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
RFileNm = "c:\temp\" & Trim(RFileNm) & ".adt"
fs.DeleteFile (RFileNm)
RSNew .Save RFileNm, adPersistADTG
RSNew .Close
RSNew .Open RFileNm, , , , adCmdFile
I think this will do what you want by doing it all at once.
Dim objPB As New PropertyBag
objPB.WriteProperty "rs", RSOld
Set RSNew = objPB.ReadProperty("rs")
Set objPB = Nothing
I don't think CLONE is going to do what you want. It just gives you another view of the same recordset you already have. This allows you to use multiple bookmarks and so forth, but the recordset is still attached to the same database the original was. I also need a way to copy the recordset and save it to a new database in a new format.

Increasing execution time of query in recordset (CursorLocation )

I am using below code to retrieve data from table.
After that i am binding resultant data to grid.
Problem is with speed.This query taking almost 2 to 3 minute to execute wherease from backend it takes 30 to 40 seconds.
-----------Current Code ------------------
rs.ActiveConnection = con //con is connection
con.Errors.Clear
rs.CursorLocation = adUseServer
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
rs.StayInSync = True
On Error Resume Next
rs.Open strCmd //strCmd is query
------- Alternate solution I tried using Client ---------------
rs.ActiveConnection = con //con is connection
con.Errors.Clear
rs.CursorLocation = adUseClient
rs.CursorType = adOpenKeyset
rs.LockType = adLockBatchOptimistic
rs.StayInSync = True
On Error Resume Next
rs.Open strCmd //strCmd is query
/// Result : Same speed
--------Alternate solution using Execute rather than Open recodset ---------------
com.ActiveConnection = con //con is connection
com.CommandType = adCmdText
com.CommandTimeout = 500
com.CommandText = strCmd //strCmd is query
con.Errors.Clear
On Error Resume Next
Set rs= com.Execute()
//Result :Speed is fast but when I try to update value in grid it is showing bellow error :
"Run-time error '3251': Current Recordset does not support
updating.This may be a limitation of the provider,or of the selected
locktype."
I think it's probably your table(s) setup / query, and not your connection that's causing the issues.
Make sure that every field listed in the WHERE, JOIN, & ORDER BY clauses are indexed.
Reevaluate any derived fields you are creating. I saw a query the other day that combined 2 text fields and then sorted on that new, -unindexed-, field.
If you post your query and your table structure, it will give me a better idea.

Script queries database but doesn't get unicode characters

I have a small vbscript file that queries a mysql database and returns a recordset which I then send to excel.
The problem is that the recordset does not return russian characters, it only returns "?" for each character.
My code is
dim adoConn
dim adoRS
dim n
set adoConn = Createobject("ADODB.Connection")
set adoRS = Createobject("ADODB.Recordset")
adoConn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=server1;DATABASE=dbtest;USER=root;PASSWORD=daveeades;OPTION=3;"
adoRS.ActiveConnection = adoConn
n=1
if adoConn.errors.count = 0 then
'now get all necessary text comments
adoRS.Open "SELECT `tbllaunchdata`.`fldResponse` FROM `tbllaunchdata`"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
While (Not adoRS.EOF)
objExcel.Cells(n, 1).Value = adoRS("fldResponse")
n = n + 1
adoRS.Movenext()
Wend
end if
adoRS.close
set adoRS=nothing
adoConn.close
set adoConn=nothing
Could anyone please help me with this, I just can't get the unicode characters showing in excel.
Many thanks
Dave
There are many possible culprits.
To start with an easy check: Start - Programs - MS Office Tools - Ms
Office Languge Settings => Is Russian enabled?
For completeness: can you use "show variables" or "\s" to make sure of the MySQL character_set_client/connection/database/... and the collations? (I can do tests with a strict utf8 config (on a linux machine)
WRT comment: can you do a test like this
Air! code:
Dim sTest : sTest = "expected russian string"
adoRS.Open "SELECT `tbllaunchdata`.`fldResponse` FROM `tbllaunchdata`"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(0, 1).Value = adoRS("fldResponse")
objExcel.Cells(1, 1).Value = sTest
objExcel.Cells(2, 1).Value = CStr( sTest = adoRS("fldResponse") )
No thanks to me: looks like the the real important item should be:
use up-to-date software components!
Hiii .. I have the sameproblem.. But i can get the currect data from DB. But while displaying it on excel cell it shows as ????...If u got any solution please let me know.. To get pass Unicode data to Ms sql server we need to Use NVarchar Data Type... with adVarWChar..
Regards,
Liyo Jose.

VBScript to export all members of multiple Active Directory groups?

Is there a way of exporting all the members of multiple Active Directory groups at once using a VBScript? Preferably the output would be the usernames listed under the group they are a member of.
I have the following which allows me to export the members of 1 AD Group at a time, but I am at a loss as to how to modify it to look at multiple groups.
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set outfile = fso.CreateTextFile("Members.csv")
Set objGroup = GetObject("LDAP://cn=*GROUPNAME*,OU=Groups,DC=domain,DC=local")
objGroup.GetInfo
arrMembersOf = objGroup.GetEx("member")
For Each GetObject in ObjGroup
outfile.WriteLine objGroup.Name
Next
For Each strMember in arrMembersOf
outfile.WriteLine strMember
Next
Any ideas?
Yeah, this is possible, but I think you might need to change your approach slightly. You need to write an LDAP query to query two groups at once, rather than just setting your scope to a particular group.
So, try reworking your script like this:
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objRootDSE = Nothing
Set ad = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
ad.ActiveConnection = adoConnection
'Put the distinguishedname of your two groups here:
strFilter = "(|(memberof=CN=Group Name,OU=....)(memberof=CN=Group Name 2,OU=....))"
'Chose what you want to return here:
strAttributes = "samaccountname,cn"
strQuery = "<LDAP://" & strDNSDomain & ">" & ";" & strFilter & ";" & strAttributes & ";subtree"
ad.CommandText = strQuery
ad.Properties("SearchScope") = 2
ad.Properties("Page Size") = 1000
ad.Properties("Cache Results") = False
Set objRS = ad.Execute
Now you've got all the results in a recordset, you can work your way through them writing each one to a file or whatever you want to do. So something like:
Do Until objRS.EOF
'Do something with each value
objRS.Fields("samaccountname")
objRS.MoveNext
Loop
Any use? I'm assuming here you know a little bit about writing LDAP queries
The best place to find scripts for Active Directory is Microsoft's Script Center Repository.
You can find a script listing all groups and all group members here ("List all groups in the domain and all members of the groups").

Resources