473,407 Members | 2,312 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,407 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 6646
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.