This should not be that hard. I'm simply trying to delete a record from a table, where the ID is equal to a form value. Done it a thousand times.
In this case, the FIRST record in the table is being deleted, NOT the selected one in the form. I've looked at table properties, form properties and tried every delete query variation I'm aware of.
I've tried building a delete query in Query Design and executing that. I've tried a SQL statement by passing the Access query. And I've tried writing the SQL to the Access delete query. All results are the same.
HOWEVER, if I hard code the ID number in the Access Delete Query, the correct record deletes as expected. Here's a few I've tried:
Set dbs = CurrentDb
DoCmd.RunSQL "DELETE * FROM " _
& "tblCourse WHERE coCourseID = " & coCourseID & ";"
Set dbs = CurrentDb
dbs.Execute "DELETE * FROM " _
& "tblCourse WHERE coCourseID = " & coCourseID & ";"
CurrentDb.QueryDefs("dqryDeleteCourse").Sql = "DELETE * FROM tblCourse WHERE
coCourseID = " & coCourseID & ";"
dbs.Execute dqryDeleteCourse
In all cases, the variable IS populating correctly.
Here's the Query Design of the dqry (this does not work):
Field: coCourseID
Table: tblCourse
Delete: WHERE
Criteria: [Forms]![frmCourseEdit]![coCourseID]
(I use this method of Criteria = form object value all throughout this application without an issue)
When Criteria = 1003 (hardcoded), it works.
Any troubleshooting ideas would be most appreciated.
Related
I am using two SQL queries in VBA that i believe they could be done in one, but I cant get it to work. I Want to turn the VBA portion into a Query outside of VBA, the VBA keeps breaking my file due to the amount of data it processes. (By break i mean it gives a message that says "this file is not a valid database" rendering the file corrupted). I search for that error but all i found was not related to breaking because of VBA code.
Anyways, here are the two queries ran with VBA.
SELECT ET.VerintEID AS EID, Sum(ET.ExceptMin)/60 AS Exeptions
FROM Tbl_VExceptTime AS ET
INNER JOIN Tbl_VCodes ON ET.Exception = Tbl_VCodes.Exception
WHERE (ET.ExceptDate Between #" & sDate & "# And #" & eDate & "#)
GROUP BY ET.VerintEID, Tbl_VCodes.IsApd
HAVING Tbl_VCodes.IsApd = ""OFF"";
I loop these results to update a table.
Do While Not .EOF
SQL = "UPDATE Tbl_AttendanceByAgent SET EXC = " & recSet.Fields(1).Value & _
" WHERE VerintID = '" & recSet.Fields(0).Value & "'"
CurrentDb.Execute SQL
.MoveNext
Loop
I know that i can save the results from the first query into a table and without looping I can update the main table with another SQL query, but I believe it can be done on a single SQL. I have tried using an UPDATE with a SELECT of the first query but it just errors out on me with an invalid syntax.
Yes this could be achieved in one single query as shown below
UPDATE Tbl_AttendanceByAgent
SET Tbl_AttendanceByAgent.EXC = t2.Exeptions
from Tbl_AttendanceByAgent t1
inner join (
SELECT ET.VerintEID AS EID, Sum(ET.ExceptMin)/60 AS Exeptions
FROM Tbl_VExceptTime AS ET
INNER JOIN Tbl_VCodes as TV ON ET.Exception = TV.Exception
WHERE (ET.ExceptDate Between #" & sDate & "# And #" & eDate & "#)
GROUP BY ET.VerintEID, TV.IsApd
HAVING Tbl_VCodes.IsApd = 'OFF'
) AS t2 on t2.EID = t1.VerintID
Note: I suppose you will replace sDate, eDate with values within your code
This question is an answer to the described errors and the given code, although it technically does not answer the request for a single SQL statement. I started adding a comment, but that's just too tedious when this answer box allows everything to be expressed efficiently at once.
First of all, referring to CurrentDb is actually NOT a basic reference to a single object instance. Rather it is more like a function call that generates a new, unique "clone" of the underlying database object. Calling it over and over again is known to produce memory leaks, and at the least is very inefficient. See MS docs for details.
Although the given code is short, it's not sweet. Not only is it repeatedly creating new database objects, it is repeatedly executing an SQL statement to update what I assume is a single row each time. That also entails regenerating the SQL string each time.
Even if executing the SQL statement repeatedly was an efficient option, there are better ways to do that, like creating a temporary (in-memory) QueryDef object with parameters. Each loop iteration then just resets the parameters and executes the same prepared SQL statement.
But in this case, it may actually be more efficient to load the table being updated into a DAO.Recordset, then use the in-memory Recordset to search for a match, then use the recordset to update the row.
I suspect that addressing a couple of those issues would make your VBA code viable.
Dim db as Database
Set db = CurrentDb 'Get just a single instance and reuse
Dim qry as QueryDef
SQL = "PARAMETERS pEXC Text ( 255 ), pID Long; " & _
" UPDATE Tbl_AttendanceByAgent SET EXC = pEXC " & _
" WHERE VerintID = pID"
set qry = db.CreateQueryDef("", SQL)
'With recSet '???
Do While Not .EOF
qry.Parameters("pEXC") = recSet.Fields(1).Value
qry.Parameters("pID") = recSet.Fields(0).Value
qry.Execute
.MoveNext
Loop
'End With recSet '???
'OR an alternative
Dim recUpdate As DAO.Recordset2
Set recUpdate = db.OpenRecordset("Tbl_AttendanceByAgent", DB_OPEN_TABLE)
Do While Not .EOF
recUpdate.FindFirst "VerintID = " & recSet.Fields(0).Value
If Not recUpdate.NoMatch Then
recUpdate.Edit
recUpdate.Fields("EXC") = recSet.Fields(1).Value
recUpdate.Update
End If
.MoveNext
Loop
I realized in commenting on Gro's answer, that the original query's aggregate clauses will produce unique values on EID, but it then becomes obvious that there is no need to group on (and sum) values which do not have Tbl_VCodes.IsApd = 'OFF'. The query would be more efficient like
SELECT ET.VerintEID AS EID, Sum(ET.ExceptMin)/60 AS Exeptions
FROM Tbl_VExceptTime AS ET
INNER JOIN Tbl_VCodes ON ET.Exception = Tbl_VCodes.Exception
WHERE (ET.ExceptDate Between #" & sDate & "# And #" & eDate & "#)
AND Tbl_VCodes.IsApd = 'OFF'
GROUP BY ET.VerintEID;
BTW, you could consider implementing the same temporary QueryDef pattern as I showed above, then you'd change the first WHERE expression to something like
PARAMETERS PsDate DateTime, PeDate DateTime;
...
WHERE (ET.ExceptDate Between [PsDate] And [PeDate])
...
Seems a small issue with an sql update query. But I can not get my head around this issue. Not very much acquainted with sql.
Based on a selection, selected rows from a table (7DARphases) are copied and inserted to another table (7tblDAR). And the Project ID (PRJID column) for all the inserted rows should be updated with a fixed value (PID) taken from the form.
Issue is: I am facing syntax issue in the part where the fixed value needs to be inserted in the PRJID column of added rows.
Code is:
Set dbs = CurrentDb
PID = Me.PRJID.Value
sqlstr = "INSERT INTO 7tblDAR (Phase, Deliverable, Link_PIM_temp, Link_PIM_WB, Description, Accept_criteria, Deliv_type, TollgateID, Workstream, Accountable, ClientRACI) SELECT [7DARphases].Phase, [7DARphases].Deliverable, [7DARphases].Link_PIM_temp, [7DARphases].Link_PIM_WB, [7DARphases].Description, [7DARphases].Accept_criteria, [7DARphases].Deliv_type, [7DARphases].TollgateID, [7DARphases].Workstream, [7DARphases].Accountable, [7DARphases].ClientRACI FROM 7DARphases WHERE ((([7DARphases].SolType) Like '*2PP*')) ORDER BY [7DARphases].Phase, [7DARphases].Deliverable;"
sqlUpdt = "update 7tblDAR set PRJID = '" & Me.PRJID.Value & "' from 7tblDAR where tblDAR.PRJID = """""
dbs.Execute sqlstr, dbFailOnError
dbs.Execute sqlUpdt, dbFailOnError
The 'sqlstr' works fine and inserts rows.
But 'sqlUpdt' gives error:
"Run-time error 3075: Syntax error (missing operator) in query expression ..."
Can you please help me out with this.
Plus, if possible, can you suggest to perform this action in one query itself.
Why not put the value in when you insert the values?
sqlstr = "INSERT INTO 7tblDAR (Phase, Deliverable, Link_PIM_temp, Link_PIM_WB, Description, Accept_criteria, Deliv_type, TollgateID, Workstream, Accountable, ClientRACI, PRJID)
SELECT . . .,
'" & Me.PRJID.Value & "'
WHERE [7DARphases].SolType Like '*2PP*')
ORDER BY [7DARphases].Phase, [7DARphases].Deliverable;"
This saves the trouble of having to update it later.
I have a table 'Contacts' which has a multivalued field 'Department'. I have another table 'Outreach' and it has a field 'partner org'. I have a query which combines 'Contacts.Department' and 'outreach.[partner org]' into one field joining the two tables using an outer join on fullname field which is common in both tables.
The SQL statement I have to combine the two fields is
Contacts.Department & ";" & Outreach.[Partner Org] AS [Dept/Partner Org]
If I run this query, I get the error saying
The multivalued field 'Contacts.Department' is not valid in the expression 'Contacts.Department & " " & Outreach.[Partner Org] AS [Dept/Partner Org]'
If I add the '.Value' to the multivalued field, I get multiple rows.
Contacts.Department.Value & " " & Outreach.[Partner Org] AS [Dept/Partner Org]
I want the output to have the multivalued valued field contents followed by a ';' and the partner org name all in the same cell.
Please can someone tell me to how to get this.
Thank You.
I've come up against a similar problem with these useful (from the end-users perspective) but irritating (from the perspective of those analysing) fields.
I came up with a workaround, using a form, that I think solves your problem. I added a pseudo-departments text field to the table, applied slightly modified code from the fourth post to the relevant field(s) "AfterUpdate" events as & when they change (I pass the current record & the SQL string only summarises that record).
Dim db As Database, _
rst As Recordset, _
strSQL As String
Me.Refresh
Set db = CurrentDb
strSQL = "Select PseudoDepartment from YOURTABLE where UNIQUEIDNO = " & Me.UNIQUEIDNO & ";"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
With rst
If .RecordCount > 0 Then
.MoveFirst
.Edit
!PseudoDepartment = MakeList(Me.UNIQUEIDNO)
.Update
End If
End With
To initialise the pseudo-departments field, one could loop through all records one by one to generate the contents. One can then query the data normally and get the results in the form you indicated.
HTH.
I am trying to delete one record in a table. I have two unbounds, one with a number and one with a date and then a command button to execute the code. So in my table I assign these values and they go in the table in their seperate column and contain the "Name, Number, Date". So say I want to delete this record, all I do is enter the number into one unbound and the date into the other unbound and then click the button and it should delete. The problem I am getting is "Error 13(Type mismatch) in procedure..." Here is my code below:
CurrentDb.Execute "DELETE FROM CrewTable WHERE KitNumber = " & Me.txtClearKitEntry And ActionDate = " & Me.txtClearDateEntry"
CurrentDb.Execute dbFailOnError
Me.Crew.Requery
Again, the KitNumber is a number and ActionDate is a date. I thought for dates you need to use '#' but I got errors with that. So I feel I just have some quotes and that kind of stuff in the wrong order. Any help would be appreciated. Thanks
Change this:
CurrentDb.Execute "DELETE FROM CrewTable WHERE KitNumber = " & Me.txtClearKitEntry And ActionDate = " & Me.txtClearDateEntry"
To this
CurrentDb.Execute "DELETE FROM CrewTable WHERE KitNumber = " & Me.txtClearKitEntry & " And ActionDate = #" & Me.txtClearDateEntry & "#;"
part of you string was not in quotes and also when using dates in sql queries in Access they have to be wrapped in #
I haven't a clue as to how to do this. I'm using Access 2007, and coding in VBA and SQL.
Table A has Data, accounts and amounts. Users can use form B to access subsets of the data in A, say, all the rows with amounts between $50 and $100.
When the user is looking at a row, I need to know if there are any other rows with the same account that are excluded from their view. In other words, I need to know if there are rows visible in the parent that aren't visible in the child.
I think a solution is to determine what filters are active on their view, and then I can use dcount to compare. I don't know how to get at the filters that are active in their view, though. And there may be an easier way - I am out of my depth here.
Let us assume that TableA has a primary key ID. Using the current event for the FormB:
Dim rs AS DAO.Recordset
dAmt = Me.Amount
sAcc = Me.Account
''Get a list of visible IDs
With Me.RecordsetClone
.MoveFirst
Do While Not .EOF
If !Amount=dAmt And !Account=sAcc Then
sIDs = sIDs & "," & .ID
End If
.MoveNext
Loop
End with
''Other IDs
sSQL = "SELECT * FROM TableA "
& "WHERE Amount = " & dAmt _
& "Account = '" & sAcc _
& "' ID Not In (" & Mid(sIDs,2) & ")"
CurrentDB.CreateQueryDef("NewQ",sSQL")
Docmd.OpenQuery NewQ
The above is untested, and as it stands, will only run once, if it runs at all, because a new query is created, rather than an existing query being edited.