VB 6 application throwing error The server principal (username) unable to access the database production under the current security context - sql

I am working with a legacy application that was written in vb 6. In the code there is an embedded sql query. I am trying to make a left join on another database. using the following select statement:
SQL = "select h.case_ref, t.t5_desc, h.narisno, h.valuation_ref, h.claimant, h.t5," & _
" h.claim_amount, h.claim_date , h.dateofentry, h.case_completed, l.project_short_name AS narisno_desc, h.case_type "
If blnFromCaseRefSelection = True Then
SQL = SQL & " from case_header h left join lut_t5 t on h.t5 = t.t5 left join" & _
" [production].[dbo].[xxx_01_project_attributes] l on h.nno = l.pin where h.case_ref = '" & cboCaseRef.Text & "'"
Else
SQL = SQL & " from lut_fileref f, case_header h left join lut_t5 t on h.t5 = t.t5" & _
" left join lut_nno n on h.nno = n.nno Where h.case_ref = f.case_ref" & _
" and f.ha_fileref = '" & xxoHAFileRef.Text & "'"
End If
error : The server principal (username) unable to access the database
production under the current security context
Any idea of how ui can solve this, my experience with VB 6 is basic

Related

Joining on 3 csv files

I need to retrieve data from 3 different CSV files (Adres.CSV, ID.CSV and Regios.CSV) and combine this in an Excel table.
This SQL statement works fine,
StrSQL = "SELECT ID.[ID], Adres.[Naam], Adres.[Adres], Adres.[Plaats] " _
& "FROM Adres.CSV Adres INNER JOIN ID.CSV ID ON Adres.[Naam] = ID.[Naam];"
as well as
StrSQL = "SELECT Adres.[Naam], Adres.[Adres], Adres.[Plaats], Regio.[Regio] " _
& "FROM Adres.CSV Adres INNER JOIN Regios.CSV Regio ON Adres.[Plaats] = Regio.[Plaats];"
but when I combine them to
StrSQL = "SELECT ID.[ID], Adres.[Naam], Adres.[Adres], Adres.[Plaats], Regio.[Regio] " _
& "FROM Adres.CSV Adres " _
& "INNER JOIN Regios.CSV Regio ON Adres.[Plaats] = Regio.[Plaats]" _
& "INNER JOIN ID.CSV ID ON Adres.[Naam] = ID.[Naam];"
I get an error message that an operator is missing.
What am I overlooking?
There are two issues here:
You are missing a space between the 3rd and 4th lines.
JET/ACE SQL requires you to use parentheses when doing multiple joins.
This should fix the sql error you are seeing:
StrSQL = "SELECT ID.[ID], Adres.[Naam], Adres.[Adres], Adres.[Plaats], Regio.[Regio] " _
& "FROM (Adres.CSV Adres " _
& "INNER JOIN Regios.CSV Regio ON Adres.[Plaats] = Regio.[Plaats]) " _
& "INNER JOIN ID.CSV ID ON Adres.[Naam] = ID.[Naam];"
Your string ends up evaluating with ...Regio.[Plaats]INNER JOIN ID.CSV.... This lack of white space causes the sql parser to miss your second INNER JOIN .

Run time error 424 .. object required

I am getting this error in Microsoft Access:
runtime error 424 object required
And below is the VBA code that I have used on the backend of a button:
VBA Code
Private Sub Command87_Click()
If Me.phase <> "" Then
Me.lst_caseItems.RowSource = " " & _
"SELECT tbl_master.fld_masterID, " & _
"tbl_master.fld_masterActionNo AS ActionNumber, " & _
"tbl_master.fld_masterStudyActionNo AS StudyActionNo, tbl_master.fld_masterIssuedTo," & _
"tbl_master.fld_masterRev AS Rev, tbl_phases.fld_phase AS Phase, tbl_studies.fld_study AS Study, " & _
"FROM ((tbl_master LEFT JOIN tbl_studies ON tbl_master.fld_studyID = ' " & tbl_studies.fld_studyID & " ') LEFT JOIN tbl_phases ON tbl_master.fld_phaseID = ' " & tbl_phases.fld_phaseID & " ') LEFT JOIN tbl_locations ON tbl_master.fld_locationID = ' " & tbl_locations.fld_locationID & " ';"
End If
End Sub
In your current code, VBA thinks tbl_studies.fld_studyID is a variable hence asking for an object.
With your current code;
' " & tbl_studies.fld_studyID & " '
The " CLOSES the SQL statement, then your ampersand tbl_studies or etc tells VBA to add in the value of the variable tbl_studies.fld_studyID then the ampersand followed by " reopens the SQL statement within VBA (so on compile all flows as necessary). As you have no variable named studyID it looks like this is the cause of your object required.
To get around this, you either need to assign a variable to the ID you want (such as dim X as long then x = studyIDyouwanthere) or write your query correctly so it links to the studies table with field studyID. Looking at it more closely it looks like you just need to write the query properly in the VBA window so it's syntactically correct (in VBA's interpretation) try the following;
Private Sub Command87_Click()
If Me.phase <> "" Then
Me.lst_caseItems.RowSource = " " & _
"SELECT tbl_master.fld_masterID, " & _
"tbl_master.fld_masterActionNo AS ActionNumber, " & _
"tbl_master.fld_masterStudyActionNo AS StudyActionNo, tbl_master.fld_masterIssuedTo," & _
"tbl_master.fld_masterRev AS Rev, tbl_phases.fld_phase AS Phase, tbl_studies.fld_study AS Study, " & _
"FROM ((tbl_master LEFT JOIN tbl_studies ON tbl_master.fld_studyID = tbl_studies.fld_studyID) LEFT JOIN tbl_phases ON tbl_master.fld_phaseID = tbl_phases.fld_phaseID ) LEFT JOIN tbl_locations ON tbl_master.fld_locationID = tbl_locations.fld_locationID;"
End If
End Sub
When you are joining on queries in VBA you don't join based on the values of variables, you'll only be using them for your select part or having/where/group BY not on the joins.

Inner Join Excel recordset with Access recordset

Is it possible to make an inner join between an excel spreadsheet and and access database table?
I tried many ways to make this possible but with no success.
This is my Visual Basic 6 code:
Dim DbTemp As Database
Dim ExcelDB As Database
DIm xlsRs As Recordset
Set DbTemp = OpenDatabase(App.Path + "\mydb.mdb")
Set ExcelDB = OpenDatabase(App.path + "\QueryExcelDATA.xls", False, True, "Excel 8.0;")
Set xlsRS = ExcelDB.OpenRecordset(query, dbOpenDynaset) ' query is a sql query I used to get some other results
Do Until xlsRs.EOF
Workspaces(0).BeginTrans
sql = "INSERT INTO Presenze(Enterprise, Emp_ID, mYear, mMonth, mDay, WorkHours) " _
& " SELECT Presenze.Emp_ID, '"+xlsRs("Entps")+"', '" + xlsRs("Yr") + "', '" + " _
& " xlsRs("Mnth") + "', '" + xlsRs("Dy") + "', '" + xlsRs("WH") + `"' " _
& " FROM [ExcelDATA$] INNER JOIN Presenze On [ExcelDATA$].[CardID] = Presenze.CardID"
DbTemp.Execute sql
Workspaces(0).CommitTrans
xlsRS.MoveNext
Loop
I want to get the data from [ExcelData$] spreadsheet and insert them to access Table "Presenze"
Can anyone help me?
The only realistic way to do this is to create an import table in your access DB and copy the data from Excel into it then join to this table. That way you don't have to recreate the join everytime the data in Excel changes.

Why does this Access query require me to log into ODBC again?

I have an Access 2010 database that has links to several ODBC tables. My username and password were cached when I first linked the tables, so I don't have to re-enter my Login ID and Password everytime I run a query - except on one. I have 20+ queries that are working just fine without having to relogin.
My SQL is below. Is there something intrinsically off in the code, or is it another issue?
SELECT DISTINCT IIf(IsNull([LeftSide]![ContactID]),"",CStr([LeftSide]![ContactID])) AS LeftSideContactID, IIf(IsNull([rightSide]![ContactID]),"",CStr([rightSide]![ContactID])) AS RightSideContactID, [RightSide]![Pref] & " & " & [LeftSide]![Pref] AS [Combined Prefix], [RightSide]![FName] & " & " & [LeftSide]![FName] AS [Combined FName], IIf([RightSide]![LName]=[LeftSide]![LName],[RightSide]![LName],[RightSide]![LName] & " & " & [LeftSide]![LName]) AS [Combined LName], [RightSide]![Pref] & " " & [RightSide]![FName] & " " & [RightSide]![LName] & " & " & [LeftSide]![Pref] & " " & [LeftSide]![FName] & " " & [LeftSide]![LName] AS [Combined Mailing Name]
FROM (([Contacts In Query (username)] AS LeftSide INNER JOIN ContactContact ON LeftSide.ContactID = ContactContact.Contact1ID) INNER JOIN [Contacts In Query (username)] AS RightSide ON ContactContact.Contact2ID = RightSide.ContactID) INNER JOIN Relationship ON ContactContact.RelationshipID = Relationship.RelationshipID
WHERE (((Relationship.Relationship) Like "*spouse*"));
[Contacts In Query (username)] is a flat table in Access, the others (Relationship and ContactContact) are linked from an external database.
I've got a workaround - oddly, as soon as I changed the joins from INNER JOIN to LEFT JOIN and RIGHT JOIN, it worked without requiring a login. I then had to use a couple of criteria to approximate an inner join by filtering out results where there wasn't an exact match.
SELECT DISTINCT IIf(IsNull([LeftSide]![ContactID]),"",CStr([LeftSide]![ContactID])) AS LeftSideContactID, IIf(IsNull([rightSide]![ContactID]),"",CStr([rightSide]![ContactID])) AS RightSideContactID, [RightSide]![Pref] & " & " & [LeftSide]![Pref] AS [Combined Prefix], [RightSide]![FName] & " & " & [LeftSide]![FName] AS [Combined FName], IIf([RightSide]![LName]=[LeftSide]![LName],[RightSide]![LName],[RightSide]![LName] & " & " & [LeftSide]![LName]) AS [Combined LName], [RightSide]![Pref] & " " & [RightSide]![FName] & " " & [RightSide]![LName] & " & " & [LeftSide]![Pref] & " " & [LeftSide]![FName] & " " & [LeftSide]![LName] AS [Combined Mailing Name]
FROM (([Contacts In Query (username)] AS LeftSide RIGHT JOIN ContactContact ON LeftSide.ContactID = ContactContact.Contact1ID) LEFT JOIN [Contacts In Query (username)] AS RightSide ON ContactContact.Contact2ID = RightSide.ContactID) INNER JOIN Relationship ON ContactContact.RelationshipID = Relationship.RelationshipID
WHERE (((IIf(IsNull([LeftSide]![ContactID]),"",CStr([LeftSide]![ContactID])))<>"") AND ((IIf(IsNull([rightSide]![ContactID]),"",CStr([rightSide]![ContactID])))<>"") AND ((Relationship.Relationship) Like "*spouse*"));
Hopefully this will help anyone who's come up against the same issue, but if anyone could shed some light on why this is happening, and why this fix works, I'd greatly appreciate it.
It sounds like there may be some database corruption in one of the hidden system tables. Have you tried importing the objects into a new Access database, and then run the original query? Corrupted objects won't import successfully.

New ODBC issue Access 2007

I have an MS Access 2007 front end to a MS SQL Server 2008R2 back end.
I've got a query that's part of a loop, and it's suddenly started generating Run-time error '3146': ODBC--call failed. errors on one query as the loop itterates. Oddly, it's not the first iteration. EDIT: When I say suddenly, it's been running like this just fine for several months (probably since March).
Set db = CurrentDb
db.QueryTimeout = 480 'thought there was a timeout issue, so set this very high
Set SupvRS = db.OpenRecordset("SELECT DISTINCT tblProcessors.Supervisor, tblProcessors.SupervisorEmail " & _
" FROM tblProcessors INNER JOIN (tblAuditPr INNER JOIN tblAuditPr_A ON tblAuditPr.PrAudit_ID = tblAuditPr_A.PrAudit_ID)" & _
" ON tblProcessors.Processor = tblAuditPr_A.Processor" & _
" WHERE tblProcessors.Supervisor IS NOT NULL " & _
" AND tblAuditPr.EndDate BETWEEN " & GetSQLDate(Me.txtFrom) & " AND " & GetSQLDate(Me.txtTo) & _
" AND tblProcessors.Processor<>'Default Processor' " & _
" AND tblAuditPr_A.Answer Not In ('NA','NF')" & _
" AND tblAuditPr.Status = 'Submitted'")
Do While Not SupvRS.EOF
'Send Supervisor Email
Attachment = PARG.GenerateAuditReport(SupvRS.Fields("Supervisor"), parSupervisor, Me.txtFrom, Me.txtTo)
Set EmailRS = db.OpenRecordset("SELECT * FROM tblProcessors WHERE Supervisor = " & GetSQLString(SupvRS.Fields("Supervisor")))
If EmailRS.EOF Then
Err.Raise -234923, Description:="Cannot find tblProcessors record for " & SupvRS.Fields("Supervisor") & "."
End If
If IsNull(EmailRS.Fields("SupervisorEmail")) Then
MailTo = <redacted>
Else
MailTo = EmailRS.Fields("SupervisorEmail")
End If
Set EmailRS = Nothing
Mailer.AddMailDocument MailTo:=MailTo, _
Subject:=Subject, _
Body:=Body, _
Attachments:=Attachment, _
From:=<redacted>
'Iterate processor emails if requested.
If Me.chkProcessor Then
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'----------------------------------------------------------------------------------------
'ERROR occurs here, but only after several supervisors have successfully passed through
Set ProcRS = db.OpenRecordset("SELECT DISTINCT tblProcessors.Processor, tblProcessors.ProcessorEmail FROM tblAuditPr" & _
" INNER JOIN (tblProcessors INNER JOIN tblAuditPr_A ON tblProcessors.Processor = tblAuditPr_A.Processor) " & _
" ON tblAuditPr.PrAudit_ID = tblAuditPr_A.PrAudit_ID" & _
" WHERE tblProcessors.Supervisor = " & GetSQLString(SupvRS.Fields("Supervisor")) & _
" AND tblAuditPr.EndDate BETWEEN " & GetSQLDate(Me.txtFrom) & " AND " & GetSQLDate(Me.txtTo))
'----------------------------------------------------------------------------------------
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Do While Not ProcRS.EOF
If Not IsNull(ProcRS.Fields("ProcessorEmail")) Then
On Error Resume Next
Attachment = PARG.GenerateAuditReport(ProcRS.Fields("Processor"), parProcessor, Me.txtFrom, Me.txtTo)
If Attachment <> "" Then
MailTo = ProcRS.Fields("ProcessorEmail")
Mailer.AddMailDocument MailTo:=MailTo, _
Subject:=Subject, _
Body:=Body, _
Attachments:=Attachment, _
From:=<redacted>
Else
Err.Clear
End If
On Error GoTo 0
End If
ProcRS.MoveNext
Loop
Set ProcRS = Nothing
End If
SupvRS.MoveNext
Loop
As noted in the code, the error occurs on an inner loop that successfully executes for other supervisors. It does consistantly break on one particular individual. Using the GetSQLString and GetSQLDate functions (returns ' delimited, double '' when necessary, pretty strings, and # delimited dates, respectivly, for passing Access queries through to SQL Server) in break mode, I duplicated the exact query that was giving me problems in an Access query. It returned a timeout error, but changing the database default timeout (and closing/opening the db) didn't resolve the issue. I pasted the exact same query into SSMS (replacing the # date delimiters with ') and it executed just fine there (taking about 1:30-2:00 to execute each try).
Taking a pause in typing up this post, I put some error trapping in to see if I could get the actual error message SQL Server was returning, when it ran without error. To me, that indicates that it is likely a time out issue. Anyone have a more educated guess as to what it actually is, or a suggestion on how to make the query more efficient so it doesn't time out?