473,698 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to lookup the last field name in a MS Access table on a form?

4 New Member
I have a table as below in test.mdb Access database. on a form, I want to use lookup to capture the the last field name in a text on the form, in this case, it is 200906.
by using some code I searched on the web site, I only can see all the field names, How can I just capture the last field name in the crosstab query.

Thanks, SunnyC
Expand|Select|Wrap|Line Numbers
  1. LocationNo  Customer No  Status  20090101  20090201  20090301  20090401  20090501  20090601
  2. 34             1018      Active         1         1         1                             1
  3. 28             1038      Active        84        41        39        31        15
  4. 21             1035      Active        14        11         6         3         1
Jun 16 '09 #1
14 11906
Frinavale
9,735 Recognized Expert Moderator Expert
Hi SunnyC!
Welcome to Bytes :)
You can find answers and ask Access questions in the Access Answers Forum.

Are you using SQL to access the lookup table?
If so could you please post it so that we can see what you're doing?

-Frinny
Jun 17 '09 #2
SunnyC
4 New Member
I searched the web and got the code for my report, but since I am a beginner. need help on how to capture the last field name and show it on a text box on a form.

here is my code:
Expand|Select|Wrap|Line Numbers
  1. Sub Test()
  2.   Dim Cn As ADODB.Connection
  3.   Dim CrossTbl As ADODB.Recordset
  4.   Dim fld As Field
  5.   Set Cn = New ADODB.Connection
  6.   Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\SunnyC\Desktop\Test.mdb;"
  7.   Cn.Open
  8.   Set CorssTbl = New ADODB.Recordset
  9.  
  10.   CrossTbl.Open "TheReport", Cn
  11.  
  12.   For Each fld In db.Recordsets("TheReport").Fields
  13.     Debug.Print fld.Name
  14.   Next fld
  15.   MyTable.Close
  16.   MyConn1.Close
  17. End Sub
Thank you for the help,

SunnyC
Jun 17 '09 #3
Frinavale
9,735 Recognized Expert Moderator Expert
That looks like VB (6) code.
Are you programming a VB6 Application or an Access application?

Do you know how to use SQL to retrieve information from a database?

I don't see where you've declared your variable "db".
Jun 17 '09 #4
ChipR
1,287 Recognized Expert Top Contributor
Here's some very crude code (that uses late binding instead of reference dependencies):
Expand|Select|Wrap|Line Numbers
  1. Dim strLastField As String
  2. Dim db As Object
  3. Dim fld As Object
  4. Dim intFieldCount As Integer
  5.  
  6. Set db = CurrentDb
  7.  
  8. 'loop through all the fields to count how many there are
  9. intFieldCount = 0
  10. For Each fld In db.TableDefs("NameOfYourTable").Fields
  11.   intFieldCount = intFieldCount + 1
  12. Next fld
  13.  
  14. If intFieldCount > 0
  15.   strLastField = db.TableDefs("NameOfYourTable").Fields(intFieldCount - 1).Name
  16. End If
Jun 17 '09 #5
SunnyC
4 New Member
I am using Access Application. I know little of SQL, do not know how to use it retrieve information form a database. can you show me how?

thanks.
Jun 17 '09 #6
Frinavale
9,735 Recognized Expert Moderator Expert
I'm sorry but I don't know much about Access (I'm mainly a .NET developer). I never knew how close the Access syntax was to VB6.

Anyways, it's always useful to know SQL (it stands for: Structured Query Language and is pronounced See-quill). SQL lets you specify what you're looking for....the database management system (in your case Access) reads the SQL you provide to it, compiles it into a command and returns any records that match what you've asked for.

For example, if you were looking for a record with the Field (named ID) with the value of 200906 you would have something like:

SELECT * FROM test WHERE id = 200906;

This statement will retrieve all of the fields (*) for any records that have a value of 200906 in the "id" field from the table named "test".

w3cschools is a great place to learn SQL.

Just be aware that SQL is always a little different for every database management system. It's supposed to be a standard so learning from w3cschools is a great idea but Access will have it's own twist on its SQL.

I should probably let the Access experts take over now :)

-Frinny

PS you may want to let the experts here know what version of Access you're using.
Jun 17 '09 #7
SunnyC
4 New Member
Thank you very much Frinny for the information and suggestions. I will take a look of it!
SunnyC
Jun 17 '09 #8
NeoPa
32,569 Recognized Expert Moderator MVP
You refer to the data source in one sentence as a table, but in another as a CrossTab query.

Any solution will need to follow a better understanding of what you require to be appropriate. If you can share that with us I'm sure we can help you.

Welcome to Bytes!
Jun 18 '09 #9
yarbrough40
320 Contributor
hey! I'm super new too... let me see if I can help... Assuming
1)the name of the query/table you are referring too is "MyTable"
2)you have an AutoNumber field in the query/table called "PrimKey" (you could also refer to any date field in the table or anything that has the create date of the record)
3) create a reference to "Microsoft Access 10.0 Object Library"
4) your textbox is called "TextBox1"

paste this code to the OnClick event of a button on your form:
Expand|Select|Wrap|Line Numbers
  1. Dim Con As ADODB.Connection
  2. Set Con = CurrentProject.Connection
  3. Dim rs As New ADODB.Recordset
  4. Dim sql As String
  5. sql = "select * from MyTable order by PrimKey desc"
  6. With rs
  7.     .ActiveConnection = Con
  8.     .Open (sql)
  9.     End With
  10.  
  11.     Textbox1.text = rs.fields("locationNo").Value
  12. End Sub
Jun 21 '09 #10

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

Similar topics

3
2915
by: my-wings | last post by:
I've been reading about how evil Lookup fields in tables are, but I've got to be missing something really basic. I know this subject has been covered before, because I've just spent an hour or two reading about it on google, but there is something I still don't understand, and I'm hoping someone will be willing to explain it to me in small words. Let's say I have a table for addresses, and it includes a field for state. What I would...
1
3354
by: Zachary Turner | last post by:
I want to make a Lookup Field based on another Lookup field. In other words, I have this table A with two fields: ID and Name, where ID is an Autonumber and Name is a friendly name. Then I have a table B which links to A via a Lookup Field so that I can select the friendly name from combo box. Now, I want to have a table C which links to table B via a lookup field, and only displays the items which are selected in some combo box of some...
13
2529
by: John Baker | last post by:
Hi: I have a general question about table access and look ups. Over simplifying things,and using a very theoretical example, lets say I have two tables; Client and Bill. The client table has a unique auto number (which is indexed) and the Client name, as well as address and other key data
3
10651
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems that are hard to find. The main problem I am having right now is that I have a report that is sorted by one of these lookup fields and it only displays the record's ID number. When I add the source table to the query it makes several records...
2
7984
by: Greg Strong | last post by:
Hello All, Is it possible to change table field lookup properties in code? I've been able to change other field properties in code, however so far no luck with field lookup properties. What I've done for test purposes is use a text input file for the table field lookup properties. I thought that I'd start first by just changing the 'Display Control' property. Thanks to Allen Browne for some ideals per...
3
2376
by: dancole42 | last post by:
I'm self-taught in Access, and as such I'm missing large chunks of knowledge, so I'm hoping someone here with some training can help me. Right now I have an Invoice form with a Customer subform. Right now it's set up so that someone can enter a customer ID, say, 12345, into the invoice form and customer 12345's name, address, etc. shows up in the Customer subform. How do I create a system where a user could start a new invoice, then
2
2108
by: ApexData | last post by:
How do I use a combo-box to do a lookup on my name table “T-NAME”, and then place the values found in the lookup table (ie LNAME, FNAME, MI) into to a row in my existing continuous forms table "T-SICK" ? I will have the combo-box lookup available in each row of my continuous form bound to "T-SICK" to make this happen. Thanks
14
6554
by: Mark | last post by:
I have a table with a field that uses a combobox to populate values. The Lookup tab within table design mode is the following: Display Control Combo Box Row Source Type Table/Query Row Source SELECT .LastName, .FirstName, .EmployeeNumber FROM ; Bound Column 3 Column Count 3 Column Heads Yes Column Widths 1";1";1"
6
5179
by: David Wright | last post by:
Hello Folks I am using Microsoft Access 2000 I would be grateful if someone could help me with “Dlookup”. I tried various methods of writing Dlookup and various events to trigger it, none of which were successful. I have been able to display the required value in a ‘text box’ which is a little helpful but not ideal. I have a table called tblLogSheet. The ‘Description’ field of this table uses a lookup table called...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.