Solved

Adding data to MS access database

Posted on 2006-11-16
3
128 Views
Last Modified: 2010-04-30
I have this script that shows a users username and computername. Instead of showing the information, I would like to write it to an Access database. But unfortunately I have no idea about how to solve it.

It is supposed to be run from a login-script, so if another approch is preferable I am listening :-)

I hope someone can help.

----- EXISTING SCRIPT -----
On Error Resume Next
Set objSysInfo = CreateObject("ADSystemInfo")

arruName      = Split(objSysInfo.UserName, ",")
arrPcName     = Split(objSysInfo.ComputerName, ",")

Wscript.Echo      "Name: " & Replace(arruName(0), "CN=", "") & vbCrLf &_
          "Department: " & Replace(arrUname(1), "OU=", "") & vbCrLf &_
          "Computername: " & Replace(arrPcName(0), "CN=", "")
----- SCRIPT END -----
0
Comment
Question by:Kasper Katzmann
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
PatG042800 earned 500 total points
ID: 17966045
To open and write to the access database in a script you can use something like the following.

'this opens the database connection
set cnMyDB= CreateObject("ADODB.Connection")
cnMyDB.Provider="Microsoft.Jet.OLEDB.4.0"
cnMyDB.Open "c:\path_to_access\file.mdb"

'now we execute an insert statement against the open database, in this example the table name is "userdata" and the field names are "username" and "computername". You must change this to match your database.
sqlstring = "insert into userdata (username, computername) values (" & Chr(34) & arruName & Chr(34) & "," & Chr(34) & arrPcName & Chr(34) & ")"
cnMyDB.execute sqlstring

'clean up
cnMyDB.close

I cannot test your code above because I am not currently in a domain environment. All put together it should look something like this:

==============================
On Error Resume Next
Set objSysInfo = CreateObject("ADSystemInfo")

arruName      = Split(objSysInfo.UserName, ",")
arrPcName     = Split(objSysInfo.ComputerName, ",")

Wscript.Echo      "Name: " & Replace(arruName(0), "CN=", "") & vbCrLf &_
          "Department: " & Replace(arrUname(1), "OU=", "") & vbCrLf &_
          "Computername: " & Replace(arrPcName(0), "CN=", "")

set cnMyDB= CreateObject("ADODB.Connection")
cnMyDB.Provider="Microsoft.Jet.OLEDB.4.0"
cnMyDB.Open "c:\path_to_access\file.mdb"
sqlstring = "insert into userdata (username, computername) values (" & Chr(34) & arruName & Chr(34) & "," & Chr(34) & arrPcName & Chr(34) & ")"
cnMyDB.execute sqlstring
cnMyDB.close

==============================
0
 

Author Comment

by:Kasper Katzmann
ID: 17967225
Yep, thats it. Great and effective.

Just for the fun of it; how du I find the Users login name? objSysInfo.UserName finds the users name.
0
 

Author Comment

by:Kasper Katzmann
ID: 17967467
I made a little modification so every username is only listed once and updated everytime the user log in to a machine.


On Error Resume Next
Set objSysInfo = CreateObject("ADSystemInfo")

arruName      = Split(objSysInfo.UserName, ",")
arrPcName     = Split(objSysInfo.ComputerName, ",")

Name = Replace(arruName(0), "CN=", "")
Department = Replace(arrUname(1), "OU=", "")
PCname = Replace(arrPcName(0), "CN=", "")

set cnMyDB= CreateObject("ADODB.Connection")
cnMyDB.Provider="Microsoft.Jet.OLEDB.4.0"
cnMyDB.Open "c:\UsersAndComputers.mdb"

sql = "SELECT username, Today FROM userdata WHERE username = '" & Name & "'"
set rs = cnMyDB.Execute(sql)

if rs.eof or rs.bof then
     sqlstring = "insert into userdata (username, computername) values ('" & Name & "','" & PcName & "')"
else
     sqlstring = "update userdata set username = '" & Name & "', computername = '" & PcName & "' where username = " & Name
end if

cnMyDB.execute sqlstring
cnMyDB.close
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question