0

I have an Access database for my new gambling habit. About: Microsoft® Access® for Microsoft 365 MSO (16.0.13929.20206) 32-bit.

Each bet record in tblBets has fields for the bet receipt, date, bet amount, the contestants, etc.

I bet on tennis. I have a tblPlayers with all of the players and their home countries.

When I add a new record to tblBets, fldPlayers is populated with a drop-down list of all the players in tblPlayers. I choose "Roger Federer" or whomever I'm betting on, and also his opponent.

In Access, I have a form that allows me to query tblBets. I often query tblBets to find see which bets are still open (fldPaid is null).

Here is the problem:

That query works fine. I get a beautiful form populated with the bets that are still open, and the contestants in that bet.

BUT:

When I connect to Access from Excel, instead of getting the players' names, I get what is apparently the object number for that player. Instead of getting "Roger Federer" returned in the recordset from the query, I get something like "2397".

So I see Roger's name if I do it from the Form in Access, but I see a bunch of numbers if I do it from VBA in Excel.

What do I do?

The code obtains the records fine. But here it is if you want to see it. the query string comes from a cell in Excel, this was it: "Select * from tblBets where fldPaid is null".

Public Sub AskAccess()


    Dim strDBPath As String
    Dim strProvider As String

    'Creating objects of Connection and Recordset
    Dim connGambling As ADODB.Connection
    Dim rstQryResult As New Recordset
    Dim connString As String
    Dim strQuery As String
    Dim rngOrigin As Range
    Dim intField As Integer
    Dim intFields As Integer
    Dim intRow As Integer
    Dim intRows As Integer
    
    'This is the connection provider.
    strProvider = "Microsoft.ACE.OLEDB.12.0;"
    
    'Path to database located on my local computer, mirrored on OneDrive
    strDBPath = "C:\Users\Craig\OneDrive\My PC in the Cloud\Filing Cabinet\MS Office\Access\Gambling.accdb;Persist Security Info=False;"""
        
    'This is the connection string
    connString = "Provider=" & strProvider & "Data Source=" & strDBPath
    
    'Remember the initial cell selected in the active sheet
    Set rngOrigin = Application.Selection
    strQuery = rngOrigin.Value  'this contains the SELECT statement
    
    Set connGambling = New ADODB.Connection
    
    connGambling.Open ConnectionString:=connString
    
    rstQryResult.Open strQuery, connGambling
    
    'I had a devil of a time determining the RecordCount, so I counted them myself.
    rstQryResult.MoveFirst
    intRows = 0
    Do While Not rstQryResult.EOF
    
        intRows = intRows + 1
        rstQryResult.MoveNext
    
    Loop
    
    If intRows > 0 Then
    
        intFields = rstQryResult.Fields.Count
    
        rstQryResult.MoveFirst
        
        For intRow = 1 To intRows
        
            For intField = 1 To intFields
            
                rngOrigin(intRow + 1, intField).Value = rstQryResult(intField - 1).Value
                
            Next intField
            
            rstQryResult.MoveNext
        
        Next intRow
    
    End If
    
    'closing the connections
    rstQryResult.Close
    connGambling.Close

End Sub
June7
  • 19,874
  • 8
  • 24
  • 34
Craig
  • 61
  • 1
  • 10
  • You built a lookup field in table? Value saved is player ID not player name. Have to base recordset on a query that joins tables so you can retrieve the associated descriptive info. I NEVER build lookup fields in table. Just causes confusion, as you experienced. – June7 May 23 '21 at 21:21
  • Does this answer your question? [Import to Excel from Access via VBA with lookup columns](https://stackoverflow.com/questions/52170973/import-to-excel-from-access-via-vba-with-lookup-columns) – June7 May 23 '21 at 21:25
  • Why involve Excel? – June7 May 23 '21 at 22:03
  • Sounds like fldPlayers is a multi-value field. I also NEVER use multi-value fields. Instead of looping recordset, explore CopyFromRecordset method. No need to know record count. – June7 May 23 '21 at 23:17
  • Must join tables because lookup uses alias and when joined, MVF will be expanded to multiple records. Must understand MVF just contains pointers referencing records in a hidden system table. – June7 May 23 '21 at 23:33
  • Open recordset with `rstQryResult.Open strQuery, connGambling, adOpenStatic, adLockReadOnly` and RecordCount property should work. – June7 May 24 '21 at 01:51
  • Don't even need to know record count if you just loop with `Do While Not rstQryResult` although still need a counter for the Excel row reference - unless you use CopyFromRecordset in which case there is no looping and no incremented counter. – June7 May 24 '21 at 02:39
  • So what I'm getting here is that there is no way to get the names from tblPlayers via tblBets. The names show up in the fldPlayer1-12 and fldOpponent1-12 fields in tblBets, but when accessing tblBets using VBA from Excel these fields only provide a key referencing the names in tblPlayers. Are y'all actually saying there's no way to access the names without redesigning the database? It's not like I couldn't do that, but come on...that data already exists in a table. It seems like there must be a way to access it. – Craig May 25 '21 at 22:02
  • Did not say that at all. Pull names by building SQL that joins tables as noted in my first comment. – June7 May 25 '21 at 22:08
  • This thing doesn't show who made these comments, so I don't know who you are. That means I don't know which comments are yours. The only person who recommended anything slightly resembling what you're saying suggests that I create a new field. That's quite different than simply structuring a SQL statement, which would be my preference. The data already exists in a table, and I have a hard time justifying expanding the size of my database with duplicate data. – Craig May 26 '21 at 13:45
  • Member ID at end of each comment. Aside from question author, there is only one member making comments. – June7 May 26 '21 at 13:56
  • Did you even review the link referenced in earlier comment? – June7 May 26 '21 at 14:02

1 Answers1

0

as J7 points out the table actually holds the key value, even though it displays the alpha to you... this is the way Access defaults the set up of a lookup field type or a combobox....trying to be helpful.

depending where you are in your design phase you can alter the lookup and delete out the key field and be sure it binds on the name alpha... however if it is too late to change table structure there - you can add a new field for txtPlayerName and put a little code that puts the text value into this new field when a selection is made...

Cahaba Data
  • 624
  • 1
  • 4
  • 4
  • Issue only occurs for lookups that use alias. I advise not to save name instead of ID. Also, saving ID and name duplicates data and defeats principles of relational database. – June7 May 23 '21 at 22:07
  • There are thousands of ranked tennis players. It made sense to tie that field to tblPlayers. As the user enters the bet, it is terribly useful to have the names autofill so as to ensure that the correct participants in the contest are recorded. – Craig May 24 '21 at 23:05
  • I use Excel to design my bets. I get tournament data from the WTA and ATP websites, put them in Excel, then decide what combinations of players I want to bet on. I simply want Excel to be able to get info from Access for a multitude of reasons. – Craig May 24 '21 at 23:07
  • If the values in fldPlayer of tblBets (there are actually fldPlayer1-fldPlayer12 and fldOpponent1-fldOpponent12 for parlays of up to 12 legs) are key values (I believe they are object numbers, no?) then the question is how do I get the corresponding text name they represent? I can't go into tblPlayers with that number and get it, can I? If my query brings back 2743 or something, is there a way to go into tblPlayers and crossreference that with "Roger Federer"? Because that number isn't in any field. – Craig May 24 '21 at 23:13
  • As already advised, build SQL that joins tables. – June7 May 25 '21 at 22:10
  • "As already advised, build sql that joins tables." Uhh...where did anyone write how to do that? If that's the solution, obviously I'm here asking how to do it. – Craig May 26 '21 at 13:39
  • "How"? Use Access query builder to get correct syntax for joining tables. This is very basic functionality. Then replicate the SQL statement in VBA code. – June7 May 26 '21 at 14:01
  • Or build query object in Access and Excel VBA references query object: `SELECT * FROM myquery WHERE fldPaid Is Null`. – June7 May 26 '21 at 15:51