Solved

Adding data to MS access database

Posted on 2006-11-16
3
132 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month7 days, 22 hours left to enroll

617 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