Results 1 to 14 of 14
  1. #1
    robs23 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    109

    How to pass table fields' names from vba to query?

    HI Guys,

    And I'm again here for help
    I've been developing an Access database to keep track of my company's ongoing projects. There's also a form to browse the history of users actions within the program. It's based on table tbHistory that stores actions and parameters as numbers (for example eventId = 1 is "logged in" and eventId = 2 is "logged out"). I've been using a query to translate those numbers to text with a syntax like:
    Code:
    IIf([tbEvents].[EventId]=1 Or [tbEvents].[EventId]=2;[tbEvents].[EventDesc];IIf([tbEvents].[EventId]=5 Or [tbEvents].[EventId]=6;...
    It worked fine but eventually the expression within expression builder has grown to the level that exceeded allowed limit and I couldn't develop my statement any more.. I decided to develop a vba function that would take eventId as a parameter and would retrieve a string, here it is:

    Code:
    Public Function translateHistory(eventId As Long) As String
    
    Select Case eventId
    Case 1 To 2
    translateHistory = ""[tbEvents].[EventDesc]""
    Case 6
    translateHistory = "[tbEvents].[EventDesc] & "" <b>"" & [tbFormDesc].[FormName] & ""</b>"""
    Case 3
    translateHistory = "[tbEvents].[EventDesc] & "" o nazwie <b>"" & [tbProjects].[projectName] & ""</b>"""
    Case 4
    translateHistory = "[tbEvents].[EventDesc] & "" o nazwie <b>"" & [tbProducts].[productName] & ""</b> do projektu <b>"" & [tbProjects_1].[projectName] & ""</b>"""
    Case 7
    translateHistory = "[tbevents].[EventDesc] & "" <b>"" & [tbProjects].[projectName] & ""</b>"""
    Case 8
    translateHistory = "[tbEvents].[EventDesc] & "" <b>"" & [tbProjects].[projectName] & ""</b>"""
    Case 9
    translateHistory = "[tbEvents].[EventDesc] & "" <b>"" & [tbProducts].[productName] & ""</b>"""
    Case 10
    translateHistory = "[tbEvents].[EventDesc] & "" <b>"" & [tbProducts].[productName] & ""</b> z projektu <b>"" & [tbProjects].[projectName] & ""</b>"""
    
    
    End Select
    
    
    End Function
    And in my query I replaced that extremely long expression with just translateHistory([tbHistory].[eventId]). It seems to work, but the result it brings is a pure string with table names and fields - in other words, the query doesn't recognize it should be replaced with appropriate value. Here's the output I get:

    Click image for larger version. 

Name:	historyjpg.jpg 
Views:	31 
Size:	135.5 KB 
ID:	17816

    Of course I'd like "[tbEvents].[EventDesc]" to be replaced with appropriate value of field "EventDesc" from table "tbEvents" as it used to be.

    Robert

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,527
    You have it in quotes:
    "
    [tbevents].[EventDesc] & "" <b>"" & [tbProjects].[projectName] & ""</b>"""

    to translate it must be outside of the quotes
    [tbevents].[EventDesc] & " <b>" & [tbProjects].[projectName] & "</b>"""

  3. #3
    robs23 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    109
    But will I be able to pass it as a string then? I'm out of home and can't test it

    EDIT: I won't be able - after I've removed those quotes I get "External name not defined" error...
    Last edited by robs23; 08-15-2014 at 08:35 AM.

  4. #4
    robs23 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    109
    No one? Any other idea to get around the expression limit problem?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    Think I have something similar. I have a 'search' form with controls for user input of criteria. VBA builds search string which is used to set form Filter property. I save this search string to a table and users can re-run the search by selecting the saved string.

    Can't pull data from table by direct reference to table and field names. Need to use DLookup or open recordset object. I don't think that is applicable to this situation. You want the parameters the user input.

    How are you capturing the user action?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It looks like "Dane" is a sub form. And it looks like you have 5 or 6 tables
    tblHistory
    tblEvents
    tbFormDesc
    tbProducts
    tbProjects
    tbProjects_1 (???)

    So the history has foreign key fields from the other tables?? Why can't you add the other tables to the query to get the desc fields?

    What is the SQL of the query for "Dane"?

    It you go the VBA function route you would need to pass the foreign key fields to the function. You would have to use DLookup() function to gey the data for the fields.
    I prefer to use recordsets to get the data... see the attachment....

  7. #7
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Part of your problem is that the function translateHistory has no idea what [tbEvents].[EventDesc] or [tbProjects].[projectName]
    are. They are part of the query (I think) but the function has no way of knowing that. What you would need to do is pass those values to the function through parameters as well, as strings, in addition to EventID.

    But I'm not sure where you are using that function; if it is in the source for the Description of Event query column, then it should work with those changes.

    John


    But I'm a

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    If you want to save the descriptive text instead of the ID selected/input by user, that will require DLookup or recordset.

    translateHistory = DLookup("EventDesc", "[tbEvents]", "EventID=" & [user selected ID])

    Or if user selects event from a combobox and combobox has EventDesc field in column 2 - note that column index begins with 0:

    translateHistory = Me.comboboxname.Column(1)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    robs23 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    109
    Quote Originally Posted by ssanfu View Post
    Why can't you add the other tables to the query to get the desc fields?
    I have tbEvents in the query and I can easily access eventDescription. The problem is that eventDescription contain only general description of the event e.g. "logged in/out". For some events it's not enough, because I want to convey more information. For example eventId = 4 is eventDescription is only "added product". I want it to appear in the history with more information like "added product" & nameOfProduct & " to the project " & nameOfProject and that's the whole point.
    Quote Originally Posted by ssanfu View Post
    What is the SQL of the query for "Dane"?
    Code:
    SELECT tbHistory.Time, [tbUsers].[userName] & " " & [tbUsers].[userSurname] AS UserFullName, translateHistory([tbEvents].[EventId]) AS EventDescription, tbEvents.EventDesc, tbProjects.projectName, tbProducts.productName
    FROM (((((tbEvents RIGHT JOIN tbHistory ON tbEvents.EventId = tbHistory.EventId) LEFT JOIN tbUsers ON tbHistory.UserId = tbUsers.UserId) LEFT JOIN tbFormDesc ON tbHistory.formId = tbFormDesc.FormId) LEFT JOIN tbProjects ON tbHistory.projectId = tbProjects.prID) LEFT JOIN tbProducts ON tbHistory.productId = tbProducts.productId) LEFT JOIN tbProjects AS tbProjects_1 ON tbProducts.projectId = tbProjects_1.prID
    ORDER BY tbHistory.Time DESC;

    Quote Originally Posted by ssanfu View Post
    I prefer to use recordsets to get the data... see the attachment....
    I like recordsets as well. I had it written in similar way as you proposed, please take a look:

    Code:
    Public Function translateHistory(historyId As Long) As StringDim eventId As Integer
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM tbHistory WHERE HistoryId = " & historyId)
    If Not rs.EOF Then
    rs.MoveFirst
    eventId = rs.Fields("EventId")
    Select Case eventId
    Case 1 To 2
    translateHistory = EventDescription(eventId)
    Case 6
    translateHistory = EventDescription(eventId) & "w formularzu <b>" & formDescription(rs.Fields("formId")) & "</b>"
    Case 3
    translateHistory = EventDescription(eventId) & " o nazwie <b>" & prjName(rs.Fields("projectId")) & "</b>"
    Case 4
    translateHistory = EventDescription(eventId) & " o nazwie <b>" & productName(rs.Fields("productId")) & "</b> do projektu <b>" & prjName(rs.Fields("projectId")) & "</b>"
    Case 7
    translateHistory = EventDescription(eventId) & " <b>" & prjName(rs.Fields("projectId")) & "</b>"
    Case 8
    translateHistory = EventDescription(eventId) & " <b>" & prjName(rs.Fields("projectId")) & "</b>"
    Case 9
    translateHistory = EventDescription(eventId) & " <b>" & productName(rs.Fields("productId")) & "</b>"
    Case 10
    translateHistory = EventDescription(eventId) & " <b>" & productName(rs.Fields("productId")) & "</b> z projektu <b>" & prjName(rs.Fields("projectId")) & "/<b>"
    Case 11
    translateHistory = EventDescription(eventId) & " <b>" & stepDescription(rs.Fields("stepId")) & "</b> w ramach produktu <b>" & productName(rs.Fields("productId")) & "</b>"
    End Select
    End If
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    End Function
    
    Public Function EventDescription(eventId As Integer) As String
    
    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM tbEvents WHERE EventId = " & eventId)
    If Not rs.EOF Then
    rs.MoveFirst
    EventDescription = rs.Fields("EventDesc")
    End If
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    End Function
    
    
    Public Function formDescription(formId As Integer) As String
    
    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM tbFormDesc WHERE FormId = " & formId)
    If Not rs.EOF Then
    rs.MoveFirst
    formDescription = rs.Fields("FormName")
    End If
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    End Function
    
    
    Public Function stepDescription(stepId As Integer) As String
    
    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM tbProjectSteps WHERE stepId = " & stepId)
    If Not rs.EOF Then
    rs.MoveFirst
    stepDescription = rs.Fields("stepDescription")
    End If
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    End Function
    This code works fine. The problem, however, is that for every record from tbHistory it opens/closes 5 recordsets from tables located on a network drive and this has a huge impact of overall performance of this form.. I will have a look how DLookup affects the performance, hopefully it's better.

  10. #10
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    DLookup() is slower than using recrodsets (from what I have read - I normally don't use DLookup). DLookup is basically doing the same thing (opening recordsets).

    What are the structures of the tables "tblHistory", tblEvents, tbFormDesc, tbProducts, tbProjects, ? Could you post the BE with a couple of records? Or a BE with just the tables?
    I have another idea that shouldn't require opening recordsets (or at least a lot fewer).. bur I need to know the fields....


    BTW, "Time" is a reserved word in Access and shouldn't be used as object name (ie field name). Plus, "Time" is not very descriptive.... Time of what??

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    Any domain aggregate function can be very slow on forms/reports and in queries. Never had a problem in VBA and I have a ton of them. I doubt you will notice performance difference. You could reduce the query by just pulling necessary field instead of using wildcard.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    robs23 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    109
    Quote Originally Posted by ssanfu View Post
    What are the structures of the tables "tblHistory", tblEvents, tbFormDesc, tbProducts, tbProjects, ? Could you post the BE with a couple of records? Or a BE with just the tables?
    No problem, a copy of be (with only the necessary tables) is attached..


    Quote Originally Posted by ssanfu View Post
    BTW, "Time" is a reserved word in Access and shouldn't be used as object name (ie field name). Plus, "Time" is not very descriptive.... Time of what??
    Didn't know that. It is just a time of event. I can change it to e.g. TimeOfEvent, didn't do so as I didn't notice any problem with this yet.

    Quote Originally Posted by June7 View Post
    Any domain aggregate function can be very slow on forms/reports and in queries. Never had a problem in VBA and I have a ton of them. I doubt you will notice performance difference.
    I did that as my first guess and believe me, the drop in performance was visible. It's probably mostly because of my company's server that is really slow (to the extent that is hard to believe in these days)

    Quote Originally Posted by June7 View Post
    You could reduce the query by just pulling necessary field instead of using wildcard.
    Wildcard?
    Attached Files Attached Files

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    The * character is wildcard. Used in SQL statement it pulls all fields. Interesting about performance. Guess I have been lucky.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You have linked tables so it looks like all of the data is available. But you want the Event Description to have extra words.

    I changed the foreign key field names to avoid confusion. (I also had to create a couple of tables.)
    And there were a couple of things I didn't understand about your tables... but I think this is closer - and faster.

    BTW, here is a list of reserved words in Access & SQL: http://www.allenbrowne.com/AppIssueBadWord.html

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How do I update a Table from a Pass-Through Query
    By smc678 in forum Programming
    Replies: 6
    Last Post: 06-27-2013, 08:18 AM
  2. Update access table from a pass-through query
    By francesco in forum Access
    Replies: 3
    Last Post: 07-02-2012, 05:49 AM
  3. Replies: 3
    Last Post: 06-29-2012, 01:31 AM
  4. Query to get column names in a table
    By access in forum Queries
    Replies: 1
    Last Post: 07-06-2009, 05:10 AM
  5. Query to get column names in a table
    By access in forum Forms
    Replies: 1
    Last Post: 07-01-2009, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums