I have a form that creates two queries, exports them to Excel and then deletes them. However, when I hit an error on my exporting, it doesn't make it the deletions. How would I go about checking to see if they already exist? And if they do, delete them so I can re-create them with the new/updated data?
Code so far:
Dim qdfNewQry As Object
Dim qdfNewWS As Object
'//----- qdfNewQry
If Not IsNull(DLookup("myExportQry", "MSysObjects", "Name='myExportQry'")) Then
CurrentDb.QueryDefs.Delete qdfNewQry.Name
Set qdfNewQry = CurrentDb.CreateQueryDef("myExportQry", exportQry)
Else
Set qdfNewQry = CurrentDb.CreateQueryDef("myExportQry", exportQry)
End If
'//----- qdfNewWS
If Not IsNull(DLookup("myExportWS", "MSysObjects", "Name='myExportWS'")) Then
CurrentDb.QueryDefs.Delete qdfNewWS.Name
Set qdfNewWS = CurrentDb.CreateQueryDef("myExportWS", exportWS)
Else
Set qdfNewWS = CurrentDb.CreateQueryDef("myExportWS", exportWS)
End If
I'm getting the error "The expression you entered as a query parameter produced this error: 'myExportQry'" on the line If Not IsNull(DLookup("myExportQry", "MSysObjects", "Name='myExportQry'")) Then
I'm pretty lost it seems. Any help/advice/corrections would be greatly appreciated!
EDIT1:
Just for clarification, I'm wanting to delete the entire query. Other alternative solutions would also be welcome!
I wouldn't use the Dlookup function for this!
Private Sub Command1_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = CurrentDb()
For Each qdf In db.QueryDefs
Debug.Print qdf.Name
If qdf.Name = "strQry" Then
CurrentDb.QueryDefs.Delete "strQry"
CurrentDb.QueryDefs.Refresh
Exit For
End If
Next qdf
Set qdf = Nothing
Set db = Nothing
End Sub
DLookup uses the following format:
DLookup([Field], [Table], [Criteria])
There's no field in MSysObjects named "MyExportQuery". So, the first argument in DLookup() is wrong.
Also, remember that when you use a reserved word (like "Name"), you need to enclose it in brackets.
I think you want to say,
DLookup("[ID]", "MSysObjects", "[Name]='myExportQry'")
That should either return a number or a NULL, so that will give you what you need to determine if the query already exists.
The above is untested, but logically it makes sense to me.
I use this. If the query is not there the DeleteObject will be "skipped"
On Error Resume Next
DoCmd.DeleteObject acQuery, "myExportQry"
Set qdfTemp = CurrentDb.CreateQueryDef("myExportQry", "SELECT From ....")
Related
In my code I run a SQL statement in VBA like this:
Dim rs As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rs = CurrentDb.OpenRecordset( SQL-Statement)
This SQL-Statement either returns one value or none.
I then want to check wether a value is returned or not.
If IsError(rs.Fields("Name").Value) = True Then
MsgBox(Error)
Else
Variable = rs.Fields("Name").Value
End If
However: If there is an error the method IsError doesn't return true but the script says runtime-error 3021 and breaks.
Why does the method fail on this?
Unfortunately I have to write this on my mobile since I'm not allowed to log in on platforms from my work PC.
Ty all in Advance
You need to check the EOF property of the recordset.
Try this:
If Not rs.EOF Then Variable = rs.Fields("Name").Value
which is the same as
If Not rs.EOF Then Variable = rs![Name]
The following code is meant to compare a field value PURE_QP1 of a recordset to another field value PURE_QP1 of another second set. But i am getting end of statement expected error. My knowledge of Access vba is admittedly low.
The code is meant to first check if the productcode is present in recordset rst.
if it is, then it checks if it is compliant by finding its PURE_QP1 (which coud be more than 1) in another table. the condition for compliance is such that all its QP1s must be found in the table.
Dim db As DAO.Database
Dim rst As Recordset
Dim rst1 As Recordset
If Nz(Me!Combo_Product_number) <> "" Then
Set db = CurrentDb
Set rst = db.OpenRecordset("Q_compliant_FCM_EU", dbOpenDynaset)
Set rst1 = db.OpenRecordset("T_DOSSIER_FPL", dbOpenDynaset)
Do While Not rst.EOF
If rst.Fields("PRODUCT_CODE") = Me!Combo_Product_number Then
rst1.FindFirst "[PURE_QP1] = '"rst.Fields("PURE_QP1")"'"
If rst.NoMatch Then
MsgBox ("Product code is NOT compliant to FPL")
Exit Sub
End If
rst1.FindNext"[PURE_QP1] = '"rst.Fields("PURE_QP1")"'"
Loop
MsgBox ("Product code is compliant to FPL")
Else
MsgBox ("Product is not available in FCM_EU")
End If
End If
End Sub
Expected end of staement error is showing in
rst1.FindFirst "[PURE_QP1] = '"rst.Fields("PURE_QP1")"'"
and
rst1.FindNext"[PURE_QP1] = '"rst.Fields("PURE_QP1")"'"
You have an extra End If just before End Sub. That End If should go above Loop command to close the If rst.Fields("PRODUCT_CODE") = Me!Combo_Product_number Then if block.
Also your code regarding rst1 is wrong.
rst1.FindFirst "[PURE_QP1] = '"rst.Fields("PURE_QP1")"'"
should be
rst1.FindFirst "[PURE_QP1] = '" & rst.Fields("PURE_QP1") & "'"
the & sign to join strings are missing in your code.
PS: Have no idea what your code supposed to do, because your find first and find next logic seems to be incorrect.
I am using Access 2016 VBA. All code works fine, otherwise.
Public Function PopUp()
Dim strSQL As String
Dim rs As DAO.Recordset
Dim db As DAO.Database
strSQL = "SELECT PopUpReminders.*, PopUpReminders.ReminderCompletion, PopUpReminders.ReminderStartDate, PopUpReminders.Employee FROM PopUpReminders WHERE (((PopUpReminders.ReminderCompletion)=False) AND ((PopUpReminders.ReminderStartDate)<=Now() AND ((PopUpReminders.Employee)='" & Forms![Login]![txtUserName] & "'));"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
'Do Nothing
Else
If rs.RecordCount > 0 Then
Do
DoCmd.OpenForm "SFPopUpReminder"
Loop Until rs!ViewedRecord = True
End If
End If
rs.Close
Set rs = Nothing
End Function
The error that appears is (copied exactly)
MS VB Run-time error 3075: Syntax error in query expression
'(((PopUpReminders.ReminderCompletion)=False) And
((PopUpReminders.ReminderStartDate)<=Now() And ((PopUpReminders.Employee)='rerdeljac'));'.
Please note, "rerdeljac" is the logintext entered into the textbox on Forms![Login]![txtUserName] and which was matched to PopUpReminders.Employee; please note also that the error message does not include the remainder of the SQL code.
(PopUpReminders.Employee) is a field on a table filled only with text, and Forms![Login]![txtUserName] is a textbox on a form intended to be filled only with text characters.
The error occurs on the Set rs = db.OpenRecordset(strSQL) line.
Your statement needs one more ) right after Now() if I am counting right. Your SQL statement is overly complicated (probably because you copied it from a query you made using the GUI). This is sufficient:
"SELECT * FROM PopUpReminders WHERE ReminderCompletion=False AND ReminderStartDate<=Now() AND Employee='" & Forms![Login]![txtUserName] & "'"
This will fail if one of your users decides to type a ' (single quote) in txtUserName. You should at least change it to Replace(Forms![Login]![txtUserName],"'","''")
Also RecordCount is not reliable. You should use rs.EOF=False OR rs.BOF=False to check if any records were returned and iterate through them with rs.MoveFirst and rs.MoveNext.
Actually, it was a combination of Fionnuala's removal of column names and SunKnight0's addition of the parentheses after the Now() that cured the issue. I can't put the answer to both, and SunKnight went way above and beyond so he gets the mark. Here is the corrected code, for those who might need it in the future:
strSQL = "SELECT PopUpReminders.* FROM PopUpReminders WHERE (((PopUpReminders.ReminderCompletion)=False) AND ((PopUpReminders.ReminderStartDate)<=Now()) AND ((PopUpReminders.Employee)='" & Forms![Login]![txtUserName] & "'));"
I have searched for solution for this problem for a while now and can't seem to find anything relevant. Is it possible to change column labels (titles) in table via VBA. The only way I find to do it is to create query based on table with parameters as SQL aliases. However it adds some more elements to my already complex data base, it is something I want to avoid. I do not want to change column names. Is there any solution to this problem or should I leave it as it is now?
Thanks for the time you took reading this and for answers!
I think that is what you want, but I am never greatly in favour of adding information to a table that could confuse future users.
Option Compare Database
Option Explicit
Sub Usage()
SetProperty "Table1", "ID", "Caption", "This is an ID"
End Sub
Sub SetProperty(TableName As String, Fieldname As String, _
PropertyName As String, PropertyValue As Variant, _
Optional PropertyType As Variant = dbText)
Dim db As DAO.Database
Dim fld As DAO.Field
Dim prp As DAO.Property
On Error GoTo Err_Property
Set db = CurrentDb
Set fld = db.TableDefs(TableName).Fields(Fieldname)
fld.Properties(PropertyName) = PropertyValue
''Debug.Print fld.Properties(PropertyName)
Exit Sub
Err_Property:
' Error 3270 means that the property was not found.
If DBEngine.Errors(0).Number = 3270 Then
' Create property, set its value, and append it to the
' Properties collection.
Set prp = fld.CreateProperty(PropertyName, _
PropertyType, PropertyValue)
fld.Properties.Append prp
Resume Next
Else
MsgBox Err.Description
End If
End Sub
I'm having an issue with the following lines of code, I feel its something simple but I can't put my finger on it. I get a Run-time error '91' object variable or with block variable not set error. The error happens on the rsc.OpenRecordSet... line. BadgeV is a number, I have the DAO reference installed, It is pulling from a linked sql server table where I have a primary key and identity. What am I missing?
Function FNC_Scan()
Dim db As DAO.Database
Dim rsc As DAO.Recordset
Set db = CurrentDb()
rsc.OpenRecordset ("SELECT dbo_ScanData.CardID, dbo_ScanData.Complete FROM dbo_ScanData WHERE (((dbo_ScanData.CardID)= Forms![Scan]!BadgeV))) AND ((dbo_ScanData.Complete)=0));")
If rsc.EOF Then
MsgBox "new item"
Else
MsgBox "Append Item"
End If
Set rsc = Nothing
Set db = Nothing
End Function
Should be
set rsc = db.OpenRecordset(...)
instead of
rsc.OpenRecordset(...)