473,378 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to FindRecords Properly in VBA instead of Macro

SueHopson
47 32bit
So I have a lookup on a form that is searching for the Customer based on the following Embedded Macro

Expand|Select|Wrap|Line Numbers
  1. ="SearchForRecord
  2. ObjectType
  3. Object Name
  4. Record First
  5. Where Condition = [CustID] = " & Str(Nz([Screen].[ActiveControl],0))
It's pulling on the following data:
SELECT qry_cxLookup.CustID, qry_cxLookup.Company FROM qry_cxLookup ORDER BY qry_cxLookup.Company;

The main form is using the same query, but is only displaying the record that matches an unbound text box on my main form.
Expand|Select|Wrap|Line Numbers
  1. txtCustID=[cmb_CxLookup].[Column](0)
I added this to eliminate the form displaying the first record from table when it loads. The goal is to display the chosen company info only (fields are locked and disabled).

So here is my problem, well problems, and (as usual) I'm positive I am making it more complicated than it has to be.
  1. I hate Macros for reason 2 and would love to convert this code to VBA (but the option is not available on the macro page)
  2. I can't figure out how on the AfterUpdate macro, to get the form to Requery/Refresh to update and display the records, which it does perfectly when I update manually.
  3. I need to either know how to hide the first company from displaying until the record is selected OR
  4. I need to be able to set the value from txtCustID to CustID AfterUpdate so that the company data displays automatically.

Any help will be greeted with cheers and a big thank you!
Jan 19 '23 #1
2 6629
SueHopson
47 32bit
Ok, so I was able to lookup the resources and create the VBA Code for the Search for Record successfully

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmb_CxLookup_AfterUpdate()
  2.     DoCmd.SearchForRecord acDataForm, "frmMain", acFirst, "[CustID] = " & str(Nz([Screen].[ActiveControl], 0))
  3.     DoCmd.RefreshRecord
  4. End Sub
The default view is Single Form not Continuous. and the form is pulled from the query - queCustomers. I'm still having the same problem on Item 3 above, which is the Customer Details displaying the first record of the query, despite nothing being selected in the Customer Lookup yet (cmb_CxLookup). The CUSTID field is a primary key so this may be unavoidable based on everything I have read/tried.

These fields are all for show only (disabled and locked) and the user has to click an edit customer button to make any changes, so there's no danger of them accidentally overwriting that data. However, I've now also been asked if it's possible to filter the initial cmb_CxLookup.
All Records - Active Only - Inactive Only

I thought I might have found a viable solution with the code below, but I just can't it to filter the Customer Combo box - it does show all records.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmb_FilterStatus_Change()
  2.     Dim sqlQry As String
  3.     sqlQry = "SELECT * FROM queCustomers ORDER BY queCustomers.Company;"
  4.     Select Case Me.cmb_FilterStatus
  5.         Case 1 'All Records
  6.             sqlQry = sqlQry
  7.         Case 2 'Active Records Only
  8.             sqlQry = sqlQry & " WHERE Active = True"
  9.                 Case 2 'Active Records Only
  10.         Case 2 'Inactive Records Only
  11.             sqlQry = sqlQry & " WHERE Active = False"
  12.         Case Else
  13.             sqlQry = sqlQry
  14.     End Select
  15.     Me.cmb_CxLookup.RowSource = sqlQry
  16.     Me.cmb_CxLookup.Requery
  17. End Sub
Not sure what logic or step I am missing...
Jan 25 '23 #2
SueHopson
47 32bit
While testing the individual code lines to see if I could find my mistake in my second post, I found a workaround I liked better. The buttons are displayed with no background or border and look/act like hyperlinks on the form - they look much cleaner and much more user-friendly.

Expand|Select|Wrap|Line Numbers
  1. Private Sub lnk_AllCustomers_Click()
  2.     Me.cmb_CxLookup.RowSource = "SELECT * FROM queCustomers ORDER BY queCustomers.Company;"
  3.     Me.cmb_CxLookup.Requery
  4. End Sub
  5.  
  6. Private Sub lnk_Inactive_Click()
  7.     Me.cmb_CxLookup.RowSource = "SELECT * FROM queCustomers WHERE Active = False ORDER BY queCustomers.Company;"
  8.     Me.cmb_CxLookup.Requery
  9. End Sub
  10.  
  11. Private Sub lnkActive_Click()
  12.     Me.cmb_CxLookup.RowSource = "SELECT * FROM queCustomers WHERE Active = True ORDER BY queCustomers.Company;"
  13.     Me.cmb_CxLookup.Requery
  14. End Sub
I am still interested in knowing what I did wrong in the second port though.

So I guess I've answered all my own questions except my original #3.
Is there a way to hide the Customer Details until a selection is made from cmb_CxLookup?
Jan 25 '23 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
8
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion is showing half the range of farenheit. print...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
7
by: fei.liu | last post by:
#define ONCFILE_ERR1(funcname, name) \ { \ #ifdef DEBUG\ cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " << funcname << " " << name << endl; \ #endif \ } I want to have...
10
by: Praveen.Kumar.SP | last post by:
Hi Could anyone solve the problem for the code below The Code: #include "stdio.h" #include "iostream.h" void Temp( int a, char* str,...)
0
by: neonspark | last post by:
I'm buidling some simple macro functionality for my app so the users can record a sequence of keyboard inputs and replay them reliably via some menu. Originally, I used: protected override bool...
9
by: Francois Grieu | last post by:
Consider this macro // check if x, assumed of type unsigned char, is in range #define ISVALID(x) ((x)>=0x20 && (x)<=0x7E) Of course, this can't be safely used as in if (ISVALID(*p++)) foo();...
7
Motoma
by: Motoma | last post by:
When I use the python interpreter on my remote machine, the arrow keys do not function properly. Instead of repositioning the cursor and recalling previous commands, I get ^[[A ^[[B ^[[C ^[[D written...
6
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in...
7
by: NeverLift | last post by:
This is probably answered elsewhere, but I've searched the Web and VBA for Excel manual, find no answers. I have a VBA-coded macro in an Excel workbook that is to open another existing workbook --...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.