Filter and update access based on VBA variant (array)

poduk

New Member
Joined
Feb 12, 2017
Messages
23
Hi

Hoping someone on here can help me out with my problem.

I have posted this on the excel page but I think it maybe more linked to access now.

I have two variants (arrays) in my coding

heading() ' linked to the headings
vRecord() ' link to the value of the cell\record

I am struggling with the filter at the moment. If i use hard code, like below, it filters the database based on the LastName of jones.
Code:
recordset.Filter = "LastName = 'Jones' "
I have tried a few different styles of code
Code:
rs.Filter = "heading(1)='" & vRecord(0) & "'"
Fails with "Run time error '3001'"
and
Code:
rs.Filter = 'heading(2)=vRecord(2)'
fails with "syntax error"

any idea?

Thanks

Phil
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Some things you can do:
- next time, post the error message so that we don't have to go and figure out what the message is for the 10,000 error numbers (this one is Application-defined or object-defined error?)
- try to be consistent when posting your tests; e.g. don't change too many things at once, such as quotes and array item numbers

Did you ensure that none of the array elements were null by checking what heading(2) is for example?
If the array element was incorrect, I think the message would be 'Subscript out of range' - not likely the issue.
By 'heading' I guess you mean that's a valid field name for the recordset. The message you got indicates there is something wrong with how you're using an application object, property or method OR a field or user defined object reference is invalid - something along those lines.
 
Upvote 0
Try:
rs.Filter = heading(1) & " = " & "'" & vRecord(0) & "'"
or
rs.Filter = heading(2) & " = " & "'" & vRecord(2) & "'"


Also view your results:
Debug.Print heading(1) & " = " & "'" & vRecord(0) & "'"
Debug.Print heading(2) & " = " & "'" & vRecord(2) & "'"


Depending on what you are trying to accomplish maybe some other way is better, since this code is obviously awkward when you are building raw strings with so many pieces/parts - but not sure.
 
Last edited:
Upvote 0
Another idea :) :
Code:
myFilter = "header = 'value'"
myFilter = replace(myFilter,"value",vRecord(0),1)
myFilter = replace(myFilter,"heading",heading(1),1)
rs.Filter = myFilter

Note: working from back to front so as to minimize the possibility of catching a match on the words header or value in the actual replacement strings. That is, replacing the value first, then the header. And reminder that replace() is case sensitive on the matching.
 
Last edited:
Upvote 0
Another idea :) :
Code:
myFilter = "header = 'value'"
myFilter = replace(myFilter,"value",vRecord(0),1)
myFilter = replace(myFilter,"heading",heading(1),1)
rs.Filter = myFilter

Note: working from back to front so as to minimize the possibility of catching a match on the words header or value in the actual replacement strings. That is, replacing the value first, then the header. And reminder that replace() is case sensitive on the matching.


This worked, thank you.
 
Upvote 0

Forum statistics

Threads
1,221,714
Messages
6,161,467
Members
451,708
Latest member
PedroMoss2268

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top