Cannot Update.Database or object is read only[Error 3027] - sql

I'm facing problem when i tried to delete data from database.I'm using Access Database
strqry3 = "SELECT * " & _
"FROM ((tbl_l0_est INNER JOIN tbl_project_resource_matrix ON
tbl_l0_est.pr_matrix_key = tbl_project_resource_matrix.pr_matrix_key) INNER JOIN
tbl_proj_app_impacted ON tbl_project_resource_matrix.project_key =
tbl_proj_app_impacted.project_key) " & _
"WHERE app_impacted = '" & Text & "'"
Set rst3 = CurrentDb.OpenRecordset(strqry3)
If rst3.EOF = True Then
'MsgBox ("")
Else
Do Until rst3.EOF
rst3.Delete
rst3.MoveNext
Loop
End If
Please Advise

The fact is that you cant delete records in that way (calling .delete method) from a composte SELECT. That is valid only in select * from onlyOneTable where some=condition
The same even occurs with other databases using for example ADO .delete method, which fails. The reason is that is no clear to the RDBM to which table .delete refers to.

Related

Executing a SQL query from vba results with nothing

Cheers,
I am trying to send a simple SQL query, but I get no results in my recordset as if it simply was not executed.
Just after, I am executing a different query, which results correctly.
When running the same query from SMSS, do brings up the desired results.
Following is the code:
If CheckLogsFromRestartDay = True Then
'This line does NOT seem to work. Nothing is changed in the Recordset
Set objMyRecordset = objMyConn.Execute( _
" SELECT top 1 cast(tValue AS date) AS RestartDate " & _
" FROM settings (nolock) WHERE tkey = 'EngineStartTime' ")
'This line DOES provide an answer correctly
Set objMyRecordset = objMyConn.Execute( _
" SELECT * FROM " & LogTable & " (nolock) where Webpage like 'Engines%'" & _
" ORDER BY id desc")
sTodayMMDD = objMyRecordset!RestartDate
Else
sTodayMMDD = Format(Date, "yyyy-mm-dd")
End If

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.

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?

VB -- How to test for value not in dropdown

I have a large VB/SQL application that I have created a problem in. I have a table of procedures that have an active flag on them. This flag can be toggled through a user screen. Other screens access this same table to populate dropdowns. The values are stored in the table as the ID field (PK). The problem I have is that I am selecting the dropdown values based on the Active flag. If I select a record that has a procedure stored in it that has been made inactive, I get the old Object Not Set to an Instance.... error.
What I want to be able to do is check for the value in the record when populating the dropdown and allow the record to be displayed by bypassing the SQL error and just showing the dropdown value as blank while not altering the record itself. I hope I'm making sense. Code is below -- pretty vanilla stuff, but I'm back in app programming after being a SysAdmin for 20 years and I'm still rusty.
Library code
Public Function GetDropDownList(ByVal strDropDownList As String, _
ByRef objSession As System.Web.SessionState.HttpSessionState) As String
Dim strSQL As String = ""
Select Case strDropDownList
Case "SurgicalProcedure_ID"
strSQL = "SELECT [ID]=0,[Description]='' " _
& "UNION " _
& "SELECT [ID],[Description] " _
& "FROM dbo.viw_List_SurgicalProcedures " _
& "WHERE ISNULL(Active,0) = 1 " _
& "ORDER BY [Description] ASC"
VB code-behind
With objSqlCommand
.Connection = mobjSqlConnection
.CommandText = "SELECT * " _
& "FROM dbo.viw_tblCaseProcedure " _
& "WHERE [ID] = " & Session("CASE_ID").ToString
.CommandType = CommandType.Text
End With
objSqlDataAdapter = New SqlDataAdapter(objSqlCommand)
objSqlDataAdapter.Fill(objSqlDataSet)
objSqlDataRow = objSqlDataSet.Tables(0).Rows(0)
objDropDownList = Me.SurgicalProcedure_ID
strStringValue = objSqlDataRow("SurgicalProcedure_ID").ToString()
If strStringValue = "" Then strStringValue = "0"
objDropDownList.Items.FindByValue(strStringValue).Selected = True
Thanks in advance for any suggestions,
Joel
I'm not entirely clear on your approach, but at a guess, the problem seems to be at this line:
objDropDownList.Items.FindByValue(strStringValue).Selected = True
The start would be to do this instead to avoid the Object Not Set error:
Dim ddlItem as ListItem = objDropDownList.Items.FindByValue(strStringValue)
If ddlItem Is Nothing Then
' Insert 'blank item' code here
Else
ddlItem.Selected = True
EndIf
Hope this helps!

VBA string length problem

I have an Access application where everytime a user enters the application, it makes a temp table for that user called 'their windows login name'_Temp. In one of my reports I need to query using that table, and I can't just make a query and set it as the recourdsource of the report, since the name of the table is always different.
What I tried then was to programatically set the recordset of the report by running the query and setting the form's recordset as the query's recordset. When I tried this, it kept giving me an error about the query.
I tried to debug, and I found that the string variable isn't able to contain the whole query at once. When I ran it with break points and added a watch for the string variable, it shows me that it cuts off the query somewhere in the middle.
I've experienced this problem before, but that was with an UPDATE query. Then, I just split it into two queries and ran both of them separately. This one is a SELECT query, and there's no way I can split it. Please help!
Thank you
Heres what I've tried doing:
ReturnUserName is a function in a module that returns just the login id of the user
Private Sub Report_Open(Cancel As Integer)
Dim strQuery As String
Dim user As String
user = ReturnUserName
strQuery = "SELECT " & user & "_Temp.EmpNumber, [FName] & ' ' & [LName] AS [Employee Name], " & _
"CourseName, DateCompleted, tblEmp_SuperAdmin.[Cost Centre] " & _
"FROM (tblCourse INNER JOIN (" & user & "_Temp INNER JOIN tblEmpCourses ON " & _
user & "_Temp.EmpNumber = EmpNo) ON tblCourse.CourseID = tblEmpCourses.CourseID) " & _
"INNER JOIN tblEmp_SuperAdmin ON " & user & "_Temp.EmpNumber = tblEmp_SuperAdmin.EmpNumber" & _
"WHERE (((" & user & "_Temp.EmpNumber) = [Forms]![Reports]![txtEmpID].[Text])) " & _
"ORDER BY CourseName;"
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim rsCmd As ADODB.Command
Set rsCmd = New ADODB.Command
rsCmd.ActiveConnection = CurrentProject.Connection
rsCmd.CommandText = strQuery
rs.Open rsCmd
Me.Recordset = rs
rs.Close
End Sub
This what strQuery contains when I add a breakpoint on rsCmd.CommandText = strQuery:
SELECT myusername_Temp.EmpNumber, [FName]
& ' ' & [LName] AS [Employee Name],
CourseName, DateCompleted,
tblEmp_SuperAdmin.[Cost Centre] FROM
(tblCourse INNER JOIN (myusername_Temp
INNER JOIN tblEmpCourses ON
myusername_Temp.EmpNumber = EmpNo) ON
tblCourse.CourseID=
(It's all one line, but I've written it like this because the underscores italicize the text)
And the error I get says Run Time Error: Join not Supported.
Not quite what I was hoping for, but guessing, for:
strQuery = "long query goes here"
Try:
strQuery = "some long query goes here "
strQuery = strQuery & "more query goes here "
BASED ON NEW INFORMATION:
strQuery = "SELECT " & user & "_Temp.EmpNumber, [FName] & ' ' & [LName] AS [Employee Name], " & _
"CourseName, DateCompleted, tblEmp_SuperAdmin.[Cost Centre] " & _
"FROM (tblCourse " & _
"INNER JOIN tblEmpCourses ON tblCourse.CourseID = tblEmpCourses.CourseID) " & _
"INNER JOIN (Temp INNER JOIN tblEmp_SuperAdmin " & _
"ON Temp.EmpNumber = tblEmp_SuperAdmin.EmpNumber) " & _
"ON Temp.EmpNumber = tblEmpCourses.EmpNo " & _
"WHERE " & user & "_Temp.EmpNumber = " & [Forms]![Reports]![txtEmpID] & _
" ORDER BY CourseName;"
Note that in VBA:
& [Forms]![Reports]![txtEmpID].[Text] &
That is, the reference to the form must go outside the quotes so you get the value.
NEW INFORMATION #2
Your best bet would be to add these tables to the Access query design window and create the joins that you want, then switch to SQL view and use the string generated for you. I do not believe that the string is too long, only that the SQL is incorrect. The SQL I posted above should work, but it may not be what you want.
You can programmatically create a querydef that fits the user. So, when your report is called, you
Delete LoginName_Query_Temp (CurrentDb.QueryDefs.Delete), if it already exists.
Create the querydef (CurrentDB.CreateQueryDef), using LoginName_Temp as the table name.
Set the RecordSource of your Report to LoginName_Query_Temp.
Open the report.
I don't see what purpose the table myusername_Temp serves here. Is that where the name fields are? If so, avoid the join entirely:
Dim lngEmpNumber As Long
Dim strName As String
Dim strSQL As String
lngEmpNumber = Forms!Reports!txtEmpID
strName = DLookup("[FName] & ' ' & [LName]", "myusername_Temp", "EmpNumber=" & lngEmpNumber
strSQL = "SELECT " & Chr(34) & strName & Chr(34) & " AS [Employee Name], " & _
"CourseName, DateCompleted, tblEmp_SuperAdmin.[Cost Centre] " & _
"FROM tblCourse " & _
"INNER JOIN tblEmpCourses " & _
"ON tblCourse.CourseID = tblEmpCourses.CourseID) " & _
"INNER JOIN tblEmp_SuperAdmin " & _
"ON tblEmp_SuperAdmin.EmpNumber = tblEmpCourses.EmpNo " & _
"WHERE tblEmp_SuperAdmin.EmpNumber = " & lngEmpNumber & _
" ORDER BY CourseName;"
Now, the parentheses may need to be changed in the join (I always do my equi-joins in the Access QBE and let it take care of the getting the order and parens correct!), and my assumptions about the purpose of the temp table may be wrong, but I don't see it being used for anything other than as an intermediate link between tables, so I guessed it must be there to provide the name fields.
If that's wrong, then I'm at a loss as to why the temp table needs to be there.
Also, in your second post you referred to the control on the form as:
Forms!Reports!txtEmpID.Text
...the .Text property of Access controls is accessible only when the control has the focus. You could use the .Value property, but since that's the default property of Access controls, you should just stop after the name of the control:
Forms!Reports!txtEmpID
...you'll see this is how I did it in my suggested code.
I find the idea of your name-based temp table to be highly problematic to begin with. Temp tables don't belong in a front end, and it's not clear to me that it is actually a temp table. If it's temp data, put it in a shared table and key the record(s) to the username. Then you don't have to worry about constructing the table name on the fly.