Vba Access error: Method or data member not found - ms-access

I have been trying to open a contracts form from contracts_all page. I want to open all the records on the contracts_all form but only show the specific one clicked on. To show this one particular record, the button uses ID found in the contracts_all form.
I have managed to go this far with the help of various people in different forums but now I am getting an error which says "Compiler error; Method or data member not found"..
Please help!
Thanks in advance.
Dim Rs As Recordset
Dim Test As Integer
Dim varBookmark As Variant
DoCmd.OpenForm "Contracts"
Set Rs = Forms!Contracts.RecordsetClone
Rs.FindFirst ("[ID] = '" & Me![ID] & "'")
varBookmark = Rs.Bookmark
Forms!Contracts.Form.Bookmark = varBookmark
If Rs.NoMatch Then
MsgBox "That does not exist in this database."
Else
End If

It looks like you may not have the appropriate references set. Make sure you have Microsoft DAO checked. Note that these methods will not work in ADO. :-)
Click Tools, then References, then Microsoft DAO 3.6 (or highest version available). Not having the correct library referenced is easy to miss if you copied and pasted code into your project.
Cheers,
LC

Try replacing
Rs.FindFirst ("[ID] = '" & Me![ID] & "'")
with
Rs.FindFirst ("[ID] = '" & Forms!contracts_all![ID] & "'")
That may be slightly off, but I'm guessing it's because your active form has changed from contracts_all to Contracts, so you can no longer use the Me! reference.

Another possibility is that the fields you are attempting to clear are set to Date or Number format, for example, which was the case in my database. I did learn a lot from looking through numerous threads on this topic though!

Related

Can't find msgbox code (Deleted it) but msgbox still appears

I haven't even been able to find anyone with this problem online, let alone a solution!
I had several msgboxes in some VBA code I was writing to provide feedback for debugging. After a while the code was stable and I commented out the MsgBox lines.
One set of msgboxes wouldn't vanish though. I even deleted it rather than removing it. In the end I decided I must have a duplication somewhere, somehow, even though all the related code ended up calling this specific function where I'd removed the MsgBox.
I couldn't see any other explanation as I'd literally deleted the lines of code. I searched the entire project for fragments of the string which formed part of the MsgBox's text. Ctrl + F, set to search entire project, tried with and without pattern matching.
I found literally nothing. I decided the string must be more constructed than I thought, and instead opted to search using 'MsgBox' to search through every time I had used the command. I checked every single MsgBox in every project file and found nothing even remotely like the debugging message the code was still generating.
Anyone have any other ideas what may be going on? My best theory is that I've somehow hidden or duplicated something, but aside from depreciating a form whose code doesn't contain a single MsgBox or call code which does, I've done nothing I can think of to cause this.
I'd paste the code but you'd still just be taking my word for it that the MsgBox isn't there.
Can anyone help? It has to be hiding in there somewhere, somehow.
Many thanks,
Code where the MsgBox originally appeared (it provided feedback based on the RecordSet's NoMatch property):
Public Function createRecordSnapshot(Table As String, dbRefIn As String, payNum As String)
Dim fieldNames As String
Dim oldDataSet As String
Dim rs As Recordset
Dim fieldCount As Integer
Dim recordTot As Integer
Dim srchString As String
Set rs = CurrentDb.OpenRecordset(Table, dbOpenDynaset)
srchString = "DBRef = '" & dbRefIn & "' and PayrollRef = '" & payNum & "'"
fieldCount = rs.Fields.Count
rs.FindFirst (srchString)
Dim o As Integer
For o = 0 To fieldCount - 1
fieldNames = fieldNames & rs.Fields(o).Name & "|"
Next o
For o = 0 To fieldCount - 1
oldDataSet = oldDataSet & rs.Fields(o).Value & "|"
Next o
createRecordSnapshot = fieldNames & vbNewLine & oldDataSet
End Function
After a modification it is good practice to re-compile the code manually, as it makes running the code faster (can skip the compilation on next run) and refreshes previously compiled code. The option is in the VB Editor's Debug menu.

Access 2016 VBA .FindFirst is not finding record in recordset

I am new to programming and I'm trying my hand at writing some VBA code. I've been searching various sites and forums to help me accomplish my ultimate goal which is to have a code that will loop through a record set and update the corresponding records matching 3 criterion list boxes found on a form. Those values pass through to text boxes found on the same form where I can edit their values.
The current issue is I'm receiving the no match message box even though the record is in the record set. I'm looking for BC2 in the Day field. Unfortunately, I haven't found a similar issue on the sites and forums where I've been searching. Any assistance on this issue or in helping me accomplish my ultimate goal would be greatly appreciated! Below is the code I have thus far. A link to the picture of the record is found below the code.
Private Sub cmdSave_Click()
Set db = CurrentDb
Set rs = db.OpenRecordset("LLT_TblDayInfo", dbOpenDynaset, dbSeeChanges)
Dim strLookupValue1 As String
strLookupValue1 = "[Day]= " = Me.lstDay.Value
rs.FindFirst (strLookupValue1)
If rs.NoMatch Then
MsgBox "no records found"
Else
Do While Not rs.NoMatch
MsgBox "i found it!!!"
rs.FindNext (strLookupValue1)
Loop
rs.FindNext (strLookupValue1)
End If
End Sub
enter image description here
First thing to do is to quote your text values using the correct syntax:
strLookupValue1 = "[Day] = '" & Me!lstDay.Value & "'"

Wrong RecordCount on Filtered Form with SQL View DataSource

I am using an Access2010 project as frontend, referring to a MS SQL Server 2008 as backend. Within my Access project there is form frmKlientenÜbersicht. This form has a view abfKlientenÜbersicht as dataSource.
Now I am trying to get the current number of records showing up in my form by using this code:
Private Sub Form_Current()
Debug.Print "Form_Current: " & anzahlDatensätze
lblAnzahlDatensätze.Caption = anzahlDatensätze & " Klient(en)"
End Sub
Private Function anzahlDatensätze() As Integer
Dim rs As Recordset
Set rs = Me.RecordsetClone
rs.MoveLast
anzahlDatensätze = rs.RecordCount
End Function
This works fine until I am using some filters. If I am using any filter on my form, the number of records stays unchanged!
What do I have to change to get the current number of records showing up (if filtered or not)?
What is the reason why my code does not show the correct number of records?
EDIT: According to the given comments and answers I tried setting Count([pkKlient] onto a textbox (see new pic) and tried DCount("*", "abfKlientenÜbersicht", me.Filter) from within VBA Code.
Unfortunatelly it seems that the filterClause is not valid when using it as parameter value for DCount. (see pic for filterClause).
As you can see count(..) does not result in a correct number - and the filterClause (generated by access!!) seems not to be valid for use by DCount(..)
If someone wants to try it, on your own, just create an ADP, add a form, add a view, form dataSource is a view, set a filter, and try to get the number of records?!!
Looking forward for any comments/answers/hints!
with VBA, DCount will give you what you need
DCount("*", "MyTable", Me.Filter)
If you want to put this on the form, there's an easier way. use an unbound box, and set it to =count([FieldName])
This count should remain correct, regardless of if it's counted filtered records or not.
Some notes, there are a dozen things that could go wrong with this, it could hardly even be called tested. However, it was returning the correct count for me.
Apparently, the form filter just hides records, whereas this will apply a real filter. However, you need to get the format into the right shape for a valid filter. In the end, a WHERE statement would probably be easier.
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
With Me.Recordset
''Filter
If ApplyType = 1 Then
''Very, very roughly. Remove form name, use single quotes
''You will need a lot more code for safety
sfilter = Replace(Me.Filter, "[" & Me.Name & "].", "")
sfilter = Replace(sfilter, """", "'")
.Filter = sfilter
MsgBox "Recordset : " & Me.Recordset.RecordCount & vbCrLf _
& "Filtered : " & .RecordCount
Else
''Remove filter - ApplyType 0
.Filter = ""
End If
End With
End Sub
Additional note with similar caveats
You can also set a textbox to something on these lines:
=IIf([FilterOn]=True,DCount("id","ATable",
Replace(Replace([Filter],"[" & [Name] & "].",""),"""","'")),Count([id]))
(Remove the break in the line, it is cosmetic)

Multi-Page vs Multi-PDF Loop problems

I have a form that contains multiple partners that share a "pool". The partners are listed on a subform. After I'm done entering the information, I want a button to run a report for each of the partners with their specific information. But since it's multiple partners and I need one report for each (which I then want to email), I want to use a Loop to go through each of the partners.
EDIT1: Added entire code for review. I do have Option Explicit in and I have compiled it as well.
Private Sub btn_Run_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
strSQL = "Select * FROM Cobind_qryReport WHERE PartPoolName = """ & Me.TopLvlPoolName & """"
Debug.Print "strSQL: " & strSQL
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
On Error GoTo Err_PO_Click
If MsgBox("Do you wish to issue the cobind invites?", vbYesNo + vbQuestion, "Confirmation Required") = vbYes Then
rs.MoveFirst
Do While rs.EOF = False
DoCmd.OutputTo acOutputReport, "Cobind_rptMain", acFormatPDF,_
"K:\OB MS Admin\Postage\CoBind Opportunities\Sent Invites\" _
& rs!CatCode & "_" & rs!PartPoolName "Cobind Invite_" & _
Format(Now(), "mmddyy") & ".pdf"
DoCmd.SendObject acSendReport, "Cobind_rptMain", acFormatPDF, ,_
, , " Cobind Invite", "Please find the cobind invite attached._
Response is needed by " & [RSVP] & ". Thank you.", True
rs.MoveNext
Loop
End If
Exit_PO_Click:
MsgBox ("It didn't work")
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Sub
Err_PO_Click:
MsgBox Err.Description
Resume Exit_PO_Click
End Sub
This should allow me to create a report for each record in my query, save it to my server, then open an email to send it out. Right now, it appears that the [PartPoolName] is hanging up the code because I'm getting a "Microsoft Office Access can't find the field "|" referred to in your expression." If I take out the [PartPoolName], it'll create a PDF with four pages (each page showing a partner), where I want to end up with four separate PDFs.
The first thing you should do is add Option Explicit to the Declarations section of your module.
Then, from the Visual Basic editor's main menu, select Debug->Compile [your project name here]
Fix all the problems the compiler complains about. I suspect one of the compiler's first complaints may be triggered by this section of your code:
rs.MoveFirst
Do While Recordset.EOF = False
Do you have two recordset objects open, or one?
After you fix everything the compiler complains about, try your revised code.
If you get runtime errors, show us the exact error message and which code line is highlighted.
If the part of your code you haven't shown us includes an error hander, you can disable that error handler like so:
'On Error GoTo Err_PO_Click
Error handlers are great for production to shield users from errors. However, during development you really need to be able to identify which code line causes the error.
Alternatively, you can leave your error handler active and select Tools->Options from the editor's main menu. In the Options dialog, select the General tab, then select the "Break on All Errors" radio button and click OK. You can switch that option back to "Break on Unhandled Errors" after you finish your testing.
Update: You wrote: Right now, it appears that the [PartPoolName] is hanging up the code because I'm getting a "Microsoft Office Access can't find the field "|" referred to in your expression."
What is [PartPoolName]? If it's a field in the recordset, you can reference its value as rs!PartPoolName If it's something else, perhaps a global variable, give us more information about it.
Update2: Whenever your current code completes without error, you will hit this:
Exit_PO_Click:
MsgBox ("It didn't work")
Can that be right?
Update3: This OutputTo statement is your issue now, right?
DoCmd.OutputTo acOutputReport, "Cobind_rptMain", acFormatPDF,_
"K:\OB MS Admin\Postage\CoBind Opportunities\Sent Invites\" & _
"Cobind Invite_" & Format(Now(), "mmddyy") & ".pdf"
Cobind_rptMain is a report. It has a RowSource for its data. You're calling OutputTo with that report 4 times (once for each of the 4 rows in the recordset). Yet you expect 4 different versions of that report ... a separate report for each PartPoolName value?
To finish off the fine work by HansUp visit a page on how to print a report for a single record and how to generate reports to attach to emails. See the Emailing reports as attachments from Microsoft Access page.

MS Access search for record by textbox instead of dropdown

I'm pretty new to MS Access. I'm trying to create a simple form that will basically search for a particular record using a textbox, rather than a drop down box. Essentially a user would be able to enter an ID number and retrieve some other related Info. However, I do not want the user to be able to add any new records to the database. I've been able to get the forms to look the way I want them, but I'm not sure where to place the code (do I create a macro, insert the code into the properties of the button?) Any help is greatly appreciated!
I assume that you have bound your form to a table or a query and that you want to be able to enter the ID manually in a textbox, then press ENTER and load that record's data or display an error message if there is no such record.
As dsteele said, make sure that the form's Data property Allow Addtions is set to No to disallow users from adding records.
Then, from the AfterUpdate event of the textbox, add the following code (assuming that your textbox is named txtGoTo):
Private Sub txtGoTo_AfterUpdate()
If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
Dim rs As DAO.RecordSet
Set rs = Me.RecordsetClone
rs.FindFirst "[ID]=" & txtGoTo
If rs.NoMatch Then
MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
vbOKOnly + vbInformation
Else
Me.RecordSet.Bookmark = rs.Bookmark
End If
rs.Close
txtGoTo = Null
End Sub
Note that you will have to change the line rs.FindFirst "[ID]=" & txtGoTo to something that is adequate for your data:
"[ID]=" should be replaced by the field you want to search (it could be "[POReference]=" or something else.
if you are searching by a numeric ID, for instance because the field is an autonumber column, then the code is fine.
Otherwise, if the field you are searching on is a string (say PN12-G) then you have to change the code to:
rs.FindFirst "[ID]=""" & txtGoTo & """"
Failing to use the proper quoting (or quoting where not necessary) will result in errors of the kind Data type mismatch....
As a new user, I would recommend that you have a look at the sample NorthWind project database that is either shiped with older versions of Access or available as a template for download from Access 2007.
There a lots of techniques to learn from as a new Access developer, including other ways to implement record navigation.
Set the form property Data/'Allow Additions' to No.
Either in the AfterUpdate event of the textbox, or in the Click event of a button, you can write code or assign a macro to look up and display the record you want.