here is the scenario:
1 - i have a DB
2 - i need to retrieve a numeric value from it
3 - if this value is less than 40 then the browser is opened and will navigate to a specific website
I have made the assumption that you are using mySQL, and the mySQL 5.1 ODBC driver is required for it to work. You can get it from here (http://www.mysql.com/downloads/connector/odbc/)
This is used in a VBScript...
Query.vbs
Code:
Dim Connection
Dim Recordset
Dim SQL
SQL = "SELECT * FROM [table]"
Set Connection = WScript.CreateObject("ADODB.Connection")
Set Recordset = WScript.CreateObject("ADODB.Recordset")
Call Connection.Open("Driver={MySQL ODBC 5.1 Driver}; Server=localhost; Database=[database]; User=[username]; Password=[password]; Option=3")
Call Recordset.Open(SQL, Connection)
Do While NOT Recordset.EOF
Call MsgBox(Recordset("id"))
Call Recordset.MoveNext()
Loop
Call Recordset.Close()
Call Connection.Close()
Set Recordset = Nothing
Set Connection = Nothing
You will need to modify the connection string with your database, username and password. Also there is a MsgBox where I alert the table column value, you must change it or add additional lines for more column values.
I have tested this on my own mySQL database and it returned the correct results.
var Statement = "SELECT coalesce(TotalWork,0) AS TotalWork FROM dual as x left outer join (SELECT 1 as Y,CONVERT(INT, SUM(MSP_TimesheetActual.ActualWorkBillable)) AS TotalWork FROM MSP_EpmResource LEFT OUTER JOIN MSP_TimesheetResource INNER JOIN MSP_TimesheetActual ON MSP_TimesheetResource.ResourceNameUID = MSP_TimesheetActual.LastChangedResourceNameUID ON MSP_EpmResource.ResourceUID = MSP_TimesheetResource.ResourceUID LEFT OUTER JOIN MSP_TimesheetPeriod INNER JOIN MSP_Timesheet ON MSP_TimesheetPeriod.PeriodUID = MSP_Timesheet.PeriodUID ON MSP_TimesheetResource.ResourceNameUID = MSP_Timesheet.OwnerResourceNameUID WHERE (MSP_EpmResource.ResourceNTAccount = 'INTERCOM\%username%') AND (MSP_TimesheetActual.TimeByDay BETWEEN MSP_TimesheetPeriod.StartDate AND MSP_TimesheetPeriod.EndDate) GROUP BY MSP_TimesheetPeriod.StartDate, MSP_TimesheetPeriod.EndDate HAVING (MSP_TimesheetPeriod.StartDate <= DATEADD(d, - 7, GETDATE())) AND (MSP_TimesheetPeriod.EndDate >= DATEADD(d, - 7, GETDATE()))) as y on x.x=y.Y";
var Connection = new ActiveXObject('ADODB.Connection');
var Recordset = new ActiveXObject('ADODB.Recordset');
var Shell = new ActiveXObject('WScript.Shell');
while (!Recordset.EOF)
{
Shell.Popup(Recordset('TotalWork'), 0, 'Message', 48);
Recordset.MoveNext();
}
Recordset.Close();
Connection.Close();
but for some reasons it returns 0
and while i'm trying to run it on sql server it returns the correct value
have you any idea about where the error may be ??
good guessing this statement should retrieve the current user
i've tried to run this script:
var Statement = "select ResourceNTAccount from MSP_EpmResource";
var Connection = new ActiveXObject('ADODB.Connection');
var Recordset = new ActiveXObject('ADODB.Recordset');
var Shell = new ActiveXObject('WScript.Shell');
while (!Recordset.EOF)
{
Shell.Popup(Recordset('ResourceNTAccount'));
Recordset.MoveNext();
}
Recordset.Close();
Connection.Close();
and it works perfectly both in the server and the remote computer even in the client computer
but when i change the query to something like:
select ResourceNTAccount from MSP_EpmResource where ResourceNTAccount='someuser'
the following error ocurred:
Line:10
Char:1
Error: Either BOF or EOF is true, or the current record has been deleted. Requested operation requires a current record.
Code: 800A0BCD
Source: ADODB.Recordset
i'm sure that the user exist and i've tried to run the query on the sql server and it runs successfully
var Connection = new ActiveXObject('ADODB.Connection');
var Recordset = new ActiveXObject('ADODB.Recordset');
var Shell = new ActiveXObject('WScript.Shell');
var Environment = Shell.Environment("Process");
var Statement = 'SELECT ResourceNTAccount FROM MSP_EpmResource WHERE ResourceNTAccount LIKE "%' + Environment('UserName') + '"';
Shell.Popup(Statement, 0, 'Statement', 0);
Connection.Open('Data Source=projectserver;Initial Catalog= PWA_ProjectServer_Reporting;User ID=sa;Password=intercom.com.eg;Provider=SQLOLEDB');
Recordset.Open(Statement, Connection);
//Recordset.MoveFirst();
while (!Recordset.EOF) {
Shell.Popup(Recordset('ResourceNTAccount'));
Recordset.MoveNext();
}
Recordset.Close();
Connection.Close();
I think your MoveFirst request is causing problems, especially if there were no records found. Also, I have changed the query to request the current user of the remote computer. You can also pull their current domain but lets see if this does anything first.
The statement that will be sent to the MS SQL server will be alerted so you can check if its correct.
Line: 10
Char: 2
Error: An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name.
Code: 80040E14
Source: Microsoft OLE DB Provider for SQL Server
Bookmarks