AD queries using AD not chasing referrals - vba

I am logged into DC=domain,dc=company1,dc=com I am using VBA to query AD. I can query GC://dc=company1,dc=com with no issues. But when I try to query GC://dc=company2,dc=com I get no results. It appears that my code will not chase referrals.
I am able to run the queries using PowerShell so I know it should work. I just can't figure out how to get the VBA code to work so it chases referrals.
Here is the VBA code I am using that does not work. It just prints "not found" even though the same code does find something when using PowerShell.
Dim adConnection As ADODB.Connection
Dim adCommand As ADODB.Command
Dim adResults As ADODB.Recordset
Set adConnection = New ADODB.Connection
Set adCommand = New ADODB.Command
adConnection.ConnectionTimeout = 600
adConnection.Provider = "ADSDSOObject"
adConnection.Open "Active Directory Provider"
Set adCommand.ActiveConnection = adConnection
adCommand.Properties("Page Size") = 1000
adCommand.Properties("Size Limit") = 0
adCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
adCommand.Properties("Timeout") = 600
adCommand.Properties("Cache Results") = False
adCommand.Properties("Chase referrals") = ADS_CHASE_REFERRALS_ALWAYS
adCommand.CommandTimeout = 600
adCommand.CommandText = "<GC://dc=company2,dc=com>;(&(objectClass=user)(samAccountName=user1));samAccountName;Subtree"
Set adResults = adCommand.Execute
If Not adResults Is Nothing Then
If Not adResults.EOF Then
Debug.Print adResults.RecordCount
Else
Debug.Print "not found"
End If
adResults.Close
End If
Here is the PowerShell code that does work:
[System.DirectoryServices.DirectoryEntry] $objDERoot = New-Object System.DirectoryServices.DirectoryEntry("GC://dc=company2,dc=com")
[System.DirectoryServices.DirectorySearcher] $objSearcher = New-Object System.DirectoryServices.DirectorySearcher($objDERoot)
$objSearcher.SearchScope = "Subtree"
$objSearcher.ReferralChasing = "All"
$objSearcher.Filter = "(&(objectClass=user)(samAccountName=user1))"
$objSearcher.PropertiesToLoad.Add("samAccountName")
[System.DirectoryServices.SearchResultCollection] $colResults = $objSearcher.FindAll()
Foreach ($objResult in $colResults)
{
$objResult.Properties.Item("canonicalName")
}

Related

Run queries on remote PostgreSQL from LibreOffice Calc

My goal is to run simple queries from LibreOffice Calc and get the results from a remote PostgreSQL database.
I tried to do something similar to this answer but I get an error.
Here is what I have:
Sub GetQuery
Dim oParms(1) as new com.sun.star.beans.PropertyValue
Dim oStatement As Object
Dim oResult As Object
Dim oConnection As Object
oParms(0).Name = "user"
oParms(0).Value = "serveruser"
oParms(1).Name = "password"
oParms(1).Value = "serverpwd"
oManager = CreateUnoService("com.sun.star.sdbc.DriverManager")
sURL = "dbname=mydatabase hostaddr=X.X.X.X port=5432 user=postgresuser password=postgrespwd"
oConnection = oManager.getConnectionWithInfo(sURL, oParms())
oStatement = oConnection.createStatement()
oResult = oStatement.executeQuery("select count(*) from mytable")
MsgBox "Result: " & oResult
oStatement.close()
End Sub
When I try to run this I get "Object variable not set" on line oStatement = oConnection.createStatement().
As you can see I have very limited experience on remote database connection.

VBA Access 2007 - ODBC cannot lock all records

I'm working on a heavy project which consists in migrating Microsoft Access local tables to Microsoft SQL Server, and I'm having troubles with a specific one.
I'm using the ODBC driver to MS SQL Server to link to my table IMMATRICULATION. Its column name IMMAT is a primary key. In my VBA code, I'm running this:
Dim mydb As Database
Dim myrs As Recordset
Dim req As String
Set mydb = CurrentDb()
DoCmd.Hourglass True
req = "SELECT * FROM IMMATRICULATION WHERE IMMAT ='" & Me.Liste_CANum & "'"
Set myrs = mydb.OpenRecordset(req, dbOpenDynaset, dbDenyWrite)
If myrs.RecordCount > 0 Then
IMMAT = myrs![IMMAT]
MARQUE = myrs![MARQUE]
MODELE = myrs![MODELE]
PRIX = myrs![PRIX]
DATE = myrs![DATE]
AMO_DUREE = myrs![AMO_DUREE]
AMO_MONTANT = myrs![AMO_MONTANT]
AMO_DEBUT = myrs![AMO_DEBUT]
AMO_FIN = myrs![AMO_FIN]
ASS_MONTANT = myrs![ASS_MONTANT]
SOM = myrs![SOMMEIL]
Else
myrs.AddNew
myrs![IMMAT] = Me.Liste_CANum
myrs.Update
IMMAT = Me.Liste_CANum
MARQUE = ""
MODELE = ""
PRIX = ""
DATE = ""
AMO_DUREE = ""
AMO_MONTANT = ""
AMO_DEBUT = ""
AMO_FIN = ""
ASS_MONTANT = ""
SOM = "Non"
End If
myrs.Close
mydb.Close
DoCmd.Hourglass False
but this line of code doesn't work:
Set myrs = mydb.OpenRecordset(req, dbOpenDynaset, dbDenyWrite)
And the following error message pops-up:
ODBC -- cannot lock all records
Do you have any ideas of how to solve my problem? I tried to remove the dbDenyWrite but the problem isn't solved.
Thanks

Logon fails if two tables are in the dataSet

My report fills two tables. It works on the local server, but when published to another server it gives the following error:
Unable to connect. Login failed.
When I take off the second table and only use the first table, the report works. How can I resolve this?
objDataTable = New Data.DataTable
objDataTable.TableName = "Table"
objDataTable.Columns.Add("pes_nom", GetType(String))
objRow = objDataTable.NewRow
objRow("pes_nom") = objProposta.clsPessoa.pesNom
objDataTable.Rows.Add(objRow)
objDataSet = New Data.DataSet
objDataSet.Tables.Add(objDataTable)
If objProposta.clsDependente.DtDependentes IsNot Nothing Then
Dim dtCloned As New Data.DataTable
dtCloned = objProposta.clsDependente.DtDependentes.Clone()
dtCloned.Columns(3).DataType = System.Type.GetType("System.String")
For Each row As Data.DataRow In objProposta.clsDependente.DtDependentes.Rows
dtCloned.ImportRow(row)
Next
dtCloned.TableName = "Dependentes"
objDataSet.Tables.Add(dtCloned)
End If
Bmgviewer1.PathReport = "RptTermoAdesaoHAP.rpt"
Bmgviewer1.DataSet = objDataSet
Bmgviewer1.DataBind()
The solution was insert all data in just one table with multiple rows.
I added the following code:
If objProposta.clsDependente.DtDependentes IsNot Nothing Then
For Each row As Data.DataRow In objProposta.clsDependente.DtDependentes.Rows
objRow("dep_nom") = row.Item("dep_nom").ToString()
objRow("dep_cpf_cgc") = row.Item("dep_cpf_cgc").ToString()
objDataTable.Rows.Add(objRow)
objRow = objDataTable.NewRow
Next
Else
objDataTable.Rows.Add(objRow)
End If
And it worked.

Error on event click + Getting data from database

I'm trying to run an event, but when I run it, I get the error:
The expression On Click you entered as the event property setting
produced the following error: The Expression you entered has a
function containing the wrong number of arguments.
The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
There may have been an error evaluating the function, even, or macro.
I'd use the following code:
Public Function CH05_Generate(Sagsnr As String)
Dim WordApp As Word.Application
Dim Doc As Word.Document
Dim WordPath As String
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Set db = CurrentDb
sql = "SELECT * FROM Projektdata WHERE Sagsnr Like '" & Sagsnr & "'"
Set rst = db.OpenRecordset(sql, dbOpenDynaset)
WordPath = "My path (Can't show this"
Set WordApp = CreateObject("Word.Application")
Set Doc = WordApp.Documents.Add(WordPath)
With Doc
.FormFields("PName").Result = rst![Projektnavn]
.FormFields("text").Result = Forms![TD-E-PM200-CH05]!Kommentar
.FormFields("S3").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q1
.FormFields("S4").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q2
.FormFields("S5").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q3
.FormFields("S6").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q4
.FormFields("S7").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q5
.FormFields("S8").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q6
.FormFields("S9").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q7
.FormFields("S10").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q8
.FormFields("S11").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q9
.FormFields("S12").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q10
.FormFields("S13").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q11
.FormFields("S14").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q12
.FormFields("S15").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q13
.FormFields("S16").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q14
.FormFields("S17").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q15
.FormFields("S18").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q16
.FormFields("S19").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q17
.FormFields("S20").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q18
.FormFields("S21").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q19
.FormFields("S22").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q20
.FormFields("S23").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q21
.FormFields("S24").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q22
.FormFields("S25").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q23
.FormFields("S26").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q24
.FormFields("S27").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q25
.FormFields("S28").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q26
.FormFields("S29").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q27
.FormFields("S30").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q28
.FormFields("S31").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q29
.FormFields("S32").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q30
.FormFields("S33").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q31
.FormFields("S34").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q32
.FormFields("S35").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q33
.FormFields("S36").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q34
.FormFields("S37").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q35
End With
WordApp.visible = True
WordApp.Activate
WordApp.ActiveDocument.Protect wdAllowOnlyFormFields, True
End Function
What I want to achieve to get data from the "Projektdata" database, and get the correct data, "Projektnavn", to fill out:
.FormFields("PName").Result = rst![Projektnavn]
My database structure is like this:
"SELECT *
FROM dbo.Projektdata p
JOIN dbo.Items i ON p.Sagsnr = i.Sagsnr
WHERE ItemID =" & ItemID & " AND p.Sagsnr Like '" & Sagsnr & "'" -- change "," to AND
I call my function like this: =CH05_Generate()
Public Function CH05_Generate(Sagsnr As String, ItemID As String)
This cannot work - the function expects two parameters, you need to pass them in the function call.

VBA type mismatch error

I have no clue what this is. Apparently it has to be some syntax error of some sort but I can not figure out what it is for the life of me! All I am doing is save data into a table in SQL Server 2008 and I am using vba in microsoft dynamics great plains 2010. The specific line it is throwing an error is
rs("FREIGHT_RATE") = frmItemProfile.txtFreightRate
but it seems to change lines whenever I make an adjustment, so there is probably something wrong in this method somewhere. The error message only says "type mismatch" Any Help would be amazing.
Public Sub SaveRecord(strItemNumber As String)
Dim qry As String
Set rs = New ADODB.Recordset
'query table name
qry = "SELECT * FROM dbo.PCI_ITEM_PROFILE where ITEMNMBR = '" & strItemNumber & "'"
'open recordset
rs.Open qry, strDSNPCI, adOpenStatic, adLockPessimistic
If rs.EOF <> True Then
'Time to Update Record
rs("RCD_KEY") = frmItemProfile.txtRCDKey
If frmItemProfile.txtCopyRightDate = "" Then
rs("COPYRIGHT_DATE") = #1/1/1900#
Else
rs("COPYRIGHT_DATE") = frmItemProfile.txtCopyRightDate
End If
rs("FIRST_CATALOG") = frmItemProfile.txtFirstCatalog
rs("CATEGORY") = frmItemProfile.txtCategory
rs("SERIES_CD") = frmItemProfile.txtSeriesCD
rs("PARENT_CD") = frmItemProfile.txtParentCD
rs("TYPE") = frmItemProfile.txtType
rs("COMMODITY_CD") = frmItemProfile.txtCommodityCD
rs("BARCODE_1") = frmItemProfile.txtBarCodeOne
rs("BARCODE_2") = frmItemProfile.txtBarCodeTwo
rs("BARCODE_3") = frmItemProfile.txtBarCodeThree
rs("BARCODE_4") = frmItemProfile.txtBarCodeFour
rs("CLASS_GROUP") = frmItemProfile.cmbClassGroup
rs("FREIGHT_RATE") = frmItemProfile.txtFreightRate
rs("ITEM_LENGTH") = frmItemProfile.txtItemLength
rs("ITEM_WIDTH") = frmItemProfile.txtItemWidth
rs("ITEM_HEIGHT") = frmItemProfile.txtItemHeight
rs("USER2ENT") = frmItemProfile.txtUserEnt
rs("CREATE_DATE") = frmItemProfile.txtCreateDate
rs("MODIFDT") = frmItemProfile.txtModifyDate
rs("IN_KIT") = frmItemProfile.txtInKit
rs("IN_BOM") = frmItemProfile.txtInBom
rs("REP_COMM_PCT") = frmItemProfile.txtRepPct
rs("REP_COMM_EXCEPT") = frmItemProfile.txtRepCommExcept
rs("ROYALTY_ITEM") = frmItemProfile.txtRoyaltyItem
rs("PPC_PAGES") = frmItemProfile.txtPPCPages
rs("PPC_PAPER") = frmItemProfile.txtPPCPaper
rs("PPC_TONERCURVE") = frmItemProfile.txtPPCTonerCurve
rs("PPC_COIL") = frmItemProfile.txtPPCCoil
rs("PPC_IMPRESSIONS") = frmItemProfile.txtPPCImpressions
rs("DROP_SHIP_ITEM") = frmItemProfile.txtDropShipItem
rs("OP_CD") = frmItemProfile.txtOPCD
If frmItemProfile.txtOPDate = "" Then
rs("OP_DATE") = #1/1/1900#
Else
rs("OP_DATE") = frmItemProfile.txtOPDate
End If
rs("NOTES") = frmItemProfile.txtNotes
rs.Update
End If
rs.Close
Set rs = Nothing
End Sub
Here's a possible repro:
Dim rs
Set rs = CreateObject("ADODB.Recordset")
With rs
.Fields.Append "FREIGHT_RATE", adDouble, , 32 ' adFldIsNullable
.Open
.AddNew
rs("FREIGHT_RATE") = "fifty-five"
End With
The error I get:
Multiple-step operation generated errors. Check each status value.