|
|
|
date: Tue, 22 Jan 2008 18:03:22 -0800,
group: microsoft.public.scripting.wsh
back
Connect to access database
Generally I can get all my answers from the Windows 2000 Scripting guide.
However, on the database part, they use a DSN which I do not want to use. I
edited the code to what I thought was right but yet the script does not work,
it just goes to the last line, so I think it is something in the connection
sting where it is failing. My code is as follows:
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set oConn = CreateObject("ADODB.Connection")
Set oRS = CreateObject("ADODB.Recordset")
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;data
source=F:\Inventory\DataBase\Test_Inventory.mdb"
oConn.Open sConn
oRS.CursorLocation = adUseClient
oRS.Open "SELECT * FROM INVENTORY", oConn, adOpenStatic, adLockOptimistic
' Loop through the recordset and determine if there is already a record. If
so exit script,
' otherwise add new record
Do While NOT oRS.EOF
If sMAC = oRS("MAC_ADDY") Then
oRS.Close
oCOnn.Close
WScript.Quit
Else
MsgBox sUserName & vbCrLf & sManufacturer & " " & sModel & vbCrLf &
sSerialNumber & vbCrLf & sIP & vbCrLf & sMAC & vbCrLf & sDomain 'Used only
for testing
oRS.AddNew
oRS("SHORT") = sUserName
oRS("PC_MODEL") = sModel
oRS("PC_MAKE") = sManufacturer
oRS("SERVICE_TAG") = sSerialNumber
oRS("IP_ADDY") = sIP
oRS("MAC_ADDY") = sMAC
oRS("DOMAIN") = sDomain
oRS.Update
End If
Loop
oRS.Close
oConn.Close
MsgBox "Script Complete"
I know it is not connecting to the database because 1. it remains empty, and
2. the msgbox within the If statement should popup, which it doesn't. Any
help is appriciated.
date: Tue, 22 Jan 2008 18:03:22 -0800
author: Phyxious
Re: Connect to access database
Phyxious wrote:
> Generally I can get all my answers from the Windows 2000 Scripting guide.
> However, on the database part, they use a DSN which I do not want to use.
> I
> edited the code to what I thought was right but yet the script does not
> work,
> it just goes to the last line, so I think it is something in the
> connection
> sting where it is failing. My code is as follows:
>
> Const adOpenStatic = 3
> Const adLockOptimistic = 3
> Const adUseClient = 3
>
> Set oConn = CreateObject("ADODB.Connection")
> Set oRS = CreateObject("ADODB.Recordset")
> sConn = "Provider=Microsoft.Jet.OLEDB.4.0;data
> source=F:\Inventory\DataBase\Test_Inventory.mdb"
> oConn.Open sConn
> oRS.CursorLocation = adUseClient
>
> oRS.Open "SELECT * FROM INVENTORY", oConn, adOpenStatic, adLockOptimistic
>
> ' Loop through the recordset and determine if there is already a record.
> If
> so exit script,
> ' otherwise add new record
> Do While NOT oRS.EOF
> If sMAC = oRS("MAC_ADDY") Then
> oRS.Close
> oCOnn.Close
> WScript.Quit
> Else
> MsgBox sUserName & vbCrLf & sManufacturer & " " & sModel & vbCrLf &
> sSerialNumber & vbCrLf & sIP & vbCrLf & sMAC & vbCrLf & sDomain 'Used only
> for testing
> oRS.AddNew
> oRS("SHORT") = sUserName
> oRS("PC_MODEL") = sModel
> oRS("PC_MAKE") = sManufacturer
> oRS("SERVICE_TAG") = sSerialNumber
> oRS("IP_ADDY") = sIP
> oRS("MAC_ADDY") = sMAC
> oRS("DOMAIN") = sDomain
> oRS.Update
> End If
> Loop
>
> oRS.Close
> oConn.Close
>
> MsgBox "Script Complete"
>
> I know it is not connecting to the database because 1. it remains empty,
> and
> 2. the msgbox within the If statement should popup, which it doesn't. Any
> help is appriciated.
I use a connection string similar to:
===============
strPath = "F:\Inventory\DataBase"
strDB = "Test_Inventory.mdb"
strConnect = "DRIVER=Microsoft Access Driver (*.mdb);" _
& "FIL=MS Access;DriverId=25;DefaultDir=" _
& strPath & ";DBQ=" & strPath & "\" & strDB & ";"
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.ConnectionString = strConnect
adoConnection.Open
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
date: Tue, 22 Jan 2008 20:55:57 -0600
author: Richard Mueller [MVP]
Re: Connect to access database
"Richard Mueller [MVP]" wrote in
message news:uiTr9tWXIHA.3556@TK2MSFTNGP02.phx.gbl...
> Phyxious wrote:
>
>> Generally I can get all my answers from the Windows 2000 Scripting guide.
>> However, on the database part, they use a DSN which I do not want to use.
>> I
>> edited the code to what I thought was right but yet the script does not
>> work,
>> it just goes to the last line, so I think it is something in the
>> connection
>> sting where it is failing. My code is as follows:
>>
>> Const adOpenStatic = 3
>> Const adLockOptimistic = 3
>> Const adUseClient = 3
>>
>> Set oConn = CreateObject("ADODB.Connection")
>> Set oRS = CreateObject("ADODB.Recordset")
>> sConn = "Provider=Microsoft.Jet.OLEDB.4.0;data
>> source=F:\Inventory\DataBase\Test_Inventory.mdb"
>> oConn.Open sConn
>> oRS.CursorLocation = adUseClient
>>
>> oRS.Open "SELECT * FROM INVENTORY", oConn, adOpenStatic, adLockOptimistic
>>
>> ' Loop through the recordset and determine if there is already a record.
>> If
>> so exit script,
>> ' otherwise add new record
>> Do While NOT oRS.EOF
>> If sMAC = oRS("MAC_ADDY") Then
>> oRS.Close
>> oCOnn.Close
>> WScript.Quit
>> Else
>> MsgBox sUserName & vbCrLf & sManufacturer & " " & sModel & vbCrLf &
>> sSerialNumber & vbCrLf & sIP & vbCrLf & sMAC & vbCrLf & sDomain 'Used
>> only
>> for testing
>> oRS.AddNew
>> oRS("SHORT") = sUserName
>> oRS("PC_MODEL") = sModel
>> oRS("PC_MAKE") = sManufacturer
>> oRS("SERVICE_TAG") = sSerialNumber
>> oRS("IP_ADDY") = sIP
>> oRS("MAC_ADDY") = sMAC
>> oRS("DOMAIN") = sDomain
>> oRS.Update
>> End If
>> Loop
>>
>> oRS.Close
>> oConn.Close
>>
>> MsgBox "Script Complete"
>>
>> I know it is not connecting to the database because 1. it remains empty,
>> and
>> 2. the msgbox within the If statement should popup, which it doesn't. Any
>> help is appriciated.
>
> I use a connection string similar to:
> ===============
> strPath = "F:\Inventory\DataBase"
> strDB = "Test_Inventory.mdb"
>
> strConnect = "DRIVER=Microsoft Access Driver (*.mdb);" _
> & "FIL=MS Access;DriverId=25;DefaultDir=" _
> & strPath & ";DBQ=" & strPath & "\" & strDB & ";"
>
> Set adoConnection = CreateObject("ADODB.Connection")
> adoConnection.ConnectionString = strConnect
> adoConnection.Open
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
We don't see much of the code, such as where sMAC, etc., are assigned
values. If there is a problem in the connection string you should get an
error message. Do not use "On Error Resume Next". It will help to have the
error message.
Once you are connected I don't think your logic works. If the recordset oRS
has 20 rows, and the last row has the correct value for MAC_ADDY, the Else
will run 19 times and you will add 19 new identical rows.
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
date: Tue, 22 Jan 2008 21:38:26 -0600
author: Richard Mueller [MVP]
Re: Connect to access database
"Phyxious" wrote in message
news:5A7B90DE-E099-40E2-BE10-3F8A7788B0C9@microsoft.com...
> Generally I can get all my answers from the Windows 2000 Scripting guide.
> However, on the database part, they use a DSN which I do not want to use.
I
> edited the code to what I thought was right but yet the script does not
work,
> it just goes to the last line, so I think it is something in the
connection
> sting where it is failing. My code is as follows:
>
> Const adOpenStatic = 3
> Const adLockOptimistic = 3
> Const adUseClient = 3
>
> Set oConn = CreateObject("ADODB.Connection")
> Set oRS = CreateObject("ADODB.Recordset")
> sConn = "Provider=Microsoft.Jet.OLEDB.4.0;data
> source=F:\Inventory\DataBase\Test_Inventory.mdb"
> oConn.Open sConn
> oRS.CursorLocation = adUseClient
>
> oRS.Open "SELECT * FROM INVENTORY", oConn, adOpenStatic, adLockOptimistic
>
> ' Loop through the recordset and determine if there is already a record.
If
> so exit script,
> ' otherwise add new record
> Do While NOT oRS.EOF
> If sMAC = oRS("MAC_ADDY") Then
> oRS.Close
> oCOnn.Close
> WScript.Quit
> Else
> MsgBox sUserName & vbCrLf & sManufacturer & " " & sModel & vbCrLf &
> sSerialNumber & vbCrLf & sIP & vbCrLf & sMAC & vbCrLf & sDomain 'Used only
> for testing
> oRS.AddNew
> oRS("SHORT") = sUserName
> oRS("PC_MODEL") = sModel
> oRS("PC_MAKE") = sManufacturer
> oRS("SERVICE_TAG") = sSerialNumber
> oRS("IP_ADDY") = sIP
> oRS("MAC_ADDY") = sMAC
> oRS("DOMAIN") = sDomain
> oRS.Update
> End If
> Loop
>
> oRS.Close
> oConn.Close
>
> MsgBox "Script Complete"
>
> I know it is not connecting to the database because 1. it remains empty,
and
> 2. the msgbox within the If statement should popup, which it doesn't. Any
> help is appriciated.
How about just ensuring that undesired records are not selected, as in:
oRS.Open "SELECT * FROM INVENTORY WHERE MAC_ADDY <> '" _
& sMAC & "'", oConn, adOpenStatic, adLockOptimistic
Then remove
If sMAC = oRS("MAC_ADDY") Then
oRS.Close
oCOnn.Close
WScript.Quit
Else
and
End If
Of course, you're also missing
oRS.MoveNext
before
Loop
date: Tue, 22 Jan 2008 22:20:34 -0600
author: McKirahan
|
|