Computing.Net > Forums > Programming > SQL Server unique id field

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

SQL Server unique id field

Reply to Message Icon

Name: bclingaman
Date: December 8, 2003 at 08:13:42 Pacific
OS: 2003
CPU/Ram: 2.24/256
Comment:

The following login script adds a record that records the computername, username, and datetime of login then saves the autonumber field for reference at logout. I am moving this to a SQL server db. It works except I have been unable to retrieve the identity field. Note the SQL statement, the DISTINCT property returns the entire column. Any suggestions how I can return to this record to enter the logout datetime?
============================================
strConnString = "Provider=SQLOLEDB;Data Source=\\server\db;uid=temp;pwd=temp"
SQL = "SELECT DISTINCT ID FROM Logins"

objConn.Open "File name=" & ".\test.udl"
Set objRS = CreateObject("ADODB.Recordset")
objRS.Open "Logins", objConn, 2, 2

objRS.AddNew

objRS("ComputerName") = ComputerName
objRS("UserID") = UserID
objRS("TimeIn") = Now
AutoNum = objRS("ID")
objRS.Update


msgbox autonum

objRS.Close
========================================



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: December 8, 2003 at 10:59:16 Pacific
Reply:

Sorry, but you are wrong. the DISTICT command retrieves only ONE instance of whatever you are selecting.

SELECT DISTINCT ID FROM Logins

Returns the following:

ID
ID
ID
ID

You want to select more that just the ID, you want ComputerName, UserId, TimeIN, AND ID. To do that you need to specify that you want more than just the ID.

SELECT * FROM Logins
or
SELECT DISTINCT ID, ComputerName, UserID, TimeIN FROM Logins

Hope this helps,
Chi Happens


0

Response Number 2
Name: bac
Date: December 8, 2003 at 11:49:07 Pacific
Reply:

Running "Select DISTINCT from logins" in Query Analyzer returns the entire column, just as you indicated:
ID
ID
ID
...
I need to retrieve only the ID of the row just inserted, not the entire column. I need to come back to this record later and this is my unique key to finding it. The other fields are duplicated.


0

Response Number 3
Name: bac
Date: December 8, 2003 at 13:26:13 Pacific
Reply:

I ended up doing a search for the last record with the userid:

===========================================
strConnString = "Provider=SQLOLEDB;Data Source=\\ac2manage\logins;uid=temp;pwd=temp"

objConn.Open "File name=" & ".\test.udl"
Set objRS = CreateObject("ADODB.Recordset")
objRS.Open "Logins", objConn, 2, 2

objRS.AddNew

objRS("ComputerName") = ComputerName
objRS("UserID") = UserID
objRS("TimeIn") = Now

objRS.Update
objRS.Close
SQL = "SELECT ID FROM Logins WHERE UserID = '" & userid & "'"
objRS.Open SQL,objConn, 2, 2

Dim last

last = 0

while not objrs.eof
if objrs("ID") > last then last = objrs("ID")
objrs.movenext
wend

autonum = last

objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
=======================================


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


finding last lines of tex... Currency problems in VB6



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: SQL Server unique id field

VB, SQL server www.computing.net/answers/programming/vb-sql-server-/1159.html

SQL Server Help www.computing.net/answers/programming/sql-server-help/10797.html

MS SQL Server question www.computing.net/answers/programming/ms-sql-server-question/9497.html