Solved

Adding data to MS access database

Posted on 2006-11-16
3
126 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now