Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding data to MS access database

Posted on 2006-11-16
3
Medium Priority
?
134 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

885 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