I need to retrieve information about an employee (strUser variable stores their sAMAccountName )ID and their manager by querying the global catalog, using classic ASP. This works:
'=========Account and connection string information for LDAP=======
Set objDomain = GetObject ("GC://RootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.provider ="ADsDSOObject"
objConn.Properties("User ID") = "..." 'domain account with read access to LDAP
objConn.Properties("Password") = "..." 'domain account password
objConn.Properties("Encrypt Password") = True
objConn.open "Active Directory Provider"
Set objCom = CreateObject("ADODB.Command")
Set objCom.ActiveConnection = objConn
objCom.CommandText ="select name, givenName, sn, distinguishedName, manager, telephonenumber, mobile, mail, company, title, department, sAMAccountName,userAccountControl, msexchhidefromaddresslists FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUser+"'"
'=======Executre query on LDAP for all accounts=========
Set objRS = objCom.Execute
Now if I'm trying to use an alias for GC e.g.:
FROM 'GC://"+objADsPath+"' AS e
I get into an infinite loop.
What I need is a way to self-join query on global catalog (as e for employee, and m for manager) where e.manager = m.distinguishedName, in other words the relationship for the self-join in that the employee's manager is the manager's distinguished name.
How to do this?
I would also appreciate any hint to documentation.
Many thanks!
Another approach that works is to embed a second query for manager details within the outer query for employee details.
objCom2.CommandText ="select sAMAccountName, name, givenName, sn, distinguishedName, manager FROM 'GC://"+objADsPath+"' where distinguishedName='" + manager + "'"
I would still like to know if GC self-join query is possible or not.
Related
I am converting my Access query to SQL view. One of the Access query has where condition where a user can input values
where table1.id=[Enter the ID of the user]
Is there a way to convert a query like this to T-SQL. It is important for me to leave the prompt as it is.
Well, first, there is little reason to convert to a pass-though query.
However, SQL Server cannot prompt you in access (or say a web site that uses SQL Server). So the GUI part must be created by YOUR web site, or say access client in this case.
It is usually best to build some prompt form with a button, since those automatic prompts that Access creates are VERY poor from a UI point of view.
As noted, it is VERY likely that you can just continue to use the access query.
However, if you need a pt query, then you use access code to ask/get the prompt value, and then add that to your query.
This will work:
Dim strSQL As String
Dim invoiceNum As String
strSQL = "select * from tblInvoices where invoiceNumber = "
invoiceNum = InputBox("enter Invoice Number")
If invoiceNum = "" Then Exit Sub
strSQL = strSQL & invoicenumber
With CurrentDb.QueryDefs("qryPassR")
.SQL = strSQL
End With
' now, docmd.OpenReport, or openform or
' whatever it is you wanted to do with the sql
However, as noted, for reports etc., I would build a nice form that allows the user to enter a value. The query prompts are pure torture to your users, and they are not user friendly at all.
Also, the above assumes that you going to open some report, or some such. If you need the data returned in a reocrdset, the use this:
Dim strSQL As String
Dim invoiceNum As String
dim rst As DAO.RecordSet
strSQL = "select * from tblInvoices where invoiceNumber = "
invoiceNum = InputBox("enter Invoice Number")
If invoiceNum = "" Then Exit Sub
strSQL = strSQL & invoicenumber
With CurrentDb.QueryDefs("qryPassR")
.SQL = strSQL
Set rst = .OpenRecordset
End With
And last but not least, as others suggested here, you should consider a stored procedure with parameters, as the above is subject to SQL injection.
i am trying to update MS-Access database table with the code below using VB.net and i get this Error "Syntax error in UPDATE statement"
Dim Dcon As OleDbConnection
Dim Dcom As OleDbCommand
Dcon = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & DataSource & ";")
Dcom = New OleDbCommand("UPDATE Drivers SET ID=?,First=?,Last=?,Company=?,Addr=?,City=?,ST=?,Zip=?,MobileP=?,HomeP=?,Email=?,DL=?,DateSince=?,DateTerm=?,TruckID=?,Commants=?,Image=? WHERE ID = ID=?", Dcon)
Dcom.Parameters.AddWithValue("#ID", Label3.Text)
Dcom.Parameters.AddWithValue("#First", TextBox1.Text)
Dcom.Parameters.AddWithValue("#Last", TextBox2.Text)
Dcom.Parameters.AddWithValue("#Company", TextBox3.Text)
Dcom.Parameters.AddWithValue("#Addr", TextBox4.Text)
Dcom.Parameters.AddWithValue("#City", TextBox5.Text)
Dcom.Parameters.AddWithValue("#ST", TextBox6.Text)
Dcom.Parameters.AddWithValue("#Zip", TextBox7.Text)
Dcom.Parameters.AddWithValue("#MobileP", TextBox8.Text)
Dcom.Parameters.AddWithValue("#HomeP", TextBox9.Text)
Dcom.Parameters.AddWithValue("#Email", TextBox10.Text)
Dcom.Parameters.AddWithValue("#DL", TextBox11.Text)
Dcom.Parameters.AddWithValue("#DateSince", TextBox12.Text)
Dcom.Parameters.AddWithValue("#DateTerm", TextBox13.Text)
Dcom.Parameters.AddWithValue("#TruckID", TextBox14.Text)
Dcom.Parameters.AddWithValue("#Commants", TextBox15.Text)
Dcom.Parameters.AddWithValue("#Image", DriverImage)
Dcom.Parameters.AddWithValue("#ID", Label3.Text)
Dcom.ExecuteNonQuery()
Dcon.Close()
I spent hours on google and i am unable to resolve this issue
this are my Field name
ID, First, Last, Company, Addr, City, ST, Zip, MobileP, HomeP, Email, DL, DateSince, DateTerm, TruckID, Commants, Image they all TEXT
Can anyone could tell me what is wrong with this syntax
The words FIRST and IMAGE are reserved keywords in MS-Access Jet SQL. If you want to use them you should use square brackets around them.
Also the syntax for the where clause is wrong. (But this is probably just a typo)
Dcom = New OleDbCommand("UPDATE Drivers SET " +
"ID=?,[First]=?,Last=?,Company=?,Addr=?,City=?," +
"ST=?,Zip=?,MobileP=?,HomeP=?,Email=?,DL=?,DateSince=?," +
"DateTerm=?,TruckID=?,Commants=?,[Image]=? WHERE ID=?", Dcon)
There is a syntax problem at the end of your sql query:
... WHERE ID = ID=?
I assume that should be
WHERE ID = ?
You're adding the ID parameter to your parameter list twice, once at the beginning and end.
Dcom.Parameters.AddWithValue("#ID", Label3.Text)
...
Dcom.Parameters.AddWithValue("#ID", Label3.Text)
Get rid of one of them.
Please tell me there is a better solution to querying a remote Access database!
We are currently using this because thus far we have failed at hooking up a sp_linkedserver. I feel like it's a terrible hack and want to be done with this whole network mapping business once and for all!
'This is for access 2007
'DataBase on Local Machine
'*************************************************************************************************
'Before a conneciton is made this code sends the credentials to the remote path.
Dim objNetwork As Object
objNetwork = Microsoft.VisualBasic.CreateObject("WScript.Network")
'this line below deletes the drive if there is one so that a correct mapped drive can be created
'objNetwork.RemoveNetworkDrive("W:")
Dim DriveLetter = "W:" ' *** This drive needs to be deleted after it is created see the end of this sub for deletion
Dim RemotePath = "\\volume10\pickles\toads\rocks\sheepy\almostTodb\behindTheScenes"
Dim UserID = "sysama01\starUser"
Dim UserPWD = "secretPass"
objNetwork.MapNetworkDrive(DriveLetter, RemotePath, False, UserID, UserPWD)
'Syntax()
'****.MapNetworkDrive(strLocalDrive, strRemoteShare, [persistent], [strUser], [strPassword])
'Options:
'strLocalDrive : The drive letter (e.g. L:)
'strRemoteShare : The UNC path to the remote drive \\MyServer\MyPrinter
'(String value)
'persistent : True/False - store the mapping persistently in the users profile
'default = false
'strUser : The user name. (Optional)
'strPassword : The password. (Optional)
' Location for code http://ss64.com/vb/drivemap.html
'*************************************************************************************************
'DataBase on Network Drive
objConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=W:\SuperDeliciousFoodDB.accdb;Jet OLEDB:Database Password=databasePasswordHere")
'DataBase on Network Drive
objConn.Open()
objCmd = New OleDbCommand("SELECT * from pancakeTable, objConn)
objReader = objCmd.ExecuteReader
...
'Do more codestuffs here
objNetwork.RemoveNetworkDrive("W:")
I would suggest migrating data onto SQL Server. If you use the linked tables option, you can continue to use the forms, reports and queries in your existing Access db, while still being able to query the data remotely, from as many connections as you can.
I have been banging my head with this script and need to assistance. I am trying to create a registration form that connects to the following sql fields: Acct_ID, Username, Password, FirstName, LastName, Confirmation, RegistrationDate and AccountNum.
What I have been able to do so far is get the form inserted into the database and have a cdosys email sent out to the email address(username) with a querystring attached to a link embedded in the email. The querystring is the AccountNum field from the registration form.
What I want to try to do is update the confirmation field only in the database when the user clicks on the link which looks like this:
http://www.domainname.com/Presenter_Account_Confirm_Registration.asp?AccountNum=2152012319101300766363428210152260.
I verified that the Account Number is transferred to the confirmation page, but I am stumped as to how to update just the confirmation field in the database. Any help would be greatly appreciated. Thank you!
Making some assumptions here, that Acct_ID is an INT, is the same as AccountNum, and that you want to set Confirmation to 1:
<%
Acct_ID = Request.QueryString("AccountNum")
set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = conn ' assume ADODB.Connection has been set up
cmd.CommandType = adCmdText
sql = "UPDATE dbo.tablename SET Confirmation = 1 WHERE Acct_ID = ?"
cmd.Parameters(0).value = Acct_ID
cmd.CommandText = sql
cmd.Execute
%>
I'm trying to return values of a specific column using the below sql string, If I change it out to sql = "select * from a_page" and objRS("P_description") it returns the values but for some reason my page will not load when using the below code.
UPDATE: I turned off on error resume next and the error I'm receiving is select permission denied. How would I give myself permissions with the code below?
SQL = "select P_Name as P_Name, P_Description as P_Description from L_PagePermission inner join A_Permission on p_permissionID = pp_PermissionID inner join A_Page on P_PageID = PP_PageID where P_PageID = 84 order by p_Name"
Page_ID = 84
connectionstring = obj_ADO.getconnectionstring
Set objCon = CreateObject("ADODB.Connection")
Set objRS = CreateObject("ADODB.Recordset")
objCon.Open connectionstring
SQL = "select P_Name as P_Name, P_Description as P_Description from L_PagePermission inner join A_Permission on p_permissionID = pp_PermissionID inner join A_Page on P_PageID = PP_PageID where P_PageID = 84 order by p_Name"
objRS.open SQL, objCon
objRS.MoveFirst
while not objRS.EOF
response.Write objRS("P_Name")
objRS.MoveNext
wend
objRS.close
objCon.close
The error you get plus the code you mentioned that is working means one thing: on either L_PagePermission table or A_Permission (or both) the user passed in the connection string has no Read permissions.
To solve this, you have to either pass "stronger" user in the connection string, or grant Read permissions to the user over those tables via something like SQL Management Studio.
By the way, you can't "grant yourself permissions" through code due to obvious security reasons - permissions exist to prevent code from doing certain things in the first place.