VBA and ChatGPT

jwcolby54

Active member
Local time
Today, 06:12
Joined
May 19, 2025
Messages
326
I am doing a YouTube presentation in a couple of weeks for the Pacific Coast Access Users Group. In the last few months I have worked more and more with Chat. I am deep into a Python / SQL Server application. I do not know Python but Chat does! So I started having Chat write the code and I learned how to channel Chat in a useful direction. Cool stuff.

In order to document the SQL Server side of the house I had Chat build a handful of stored procedures to add a "Purpose" property to tables, field and views. Then Chat and I sat down and created these "purpose" properties for everything we had built so far. It worked swimmingly and was really fast to label these objects. So I decided to make that the subject of my presentation to PCAUG. Join us to discuss...

The idea is to have Chat create VBA code for a function that will take a dao SomeObject.Properties as the first parameter and a purpose string "This object does this here thing" as the second parameter. Chat wrote that, however I had planted the seed that SomeObject.Properties was just a collection with property objects in it. Which is not the case as it turned out, my bad. SomeObject.Properties is a specific thing, a DAO object not a generic collection. Long story short, it took a couple of iterations for me to straighten out that function.

I actually wrote this code while driving down the road using the phone ChatGPT voice mode where I speak and Chat responds audibly. When I got back to my laptop, the code was waiting for me. I tested it. It failed as described below, but I fed back the error messages to Chat and Chat fixed the code. Ten minutes on the phone with Chat and 10 minutes on my laptop with Chat and I had working code. Which I find unbelievably cool.

And finally I had Chat write a GetTableMetadata function to pull very basic metadata for the table just to demonstrate that the properties were in fact created.

SetTablePurpose "tlkpHairColor", "A lookup table to store hair color"
SetFieldPurpose "tlkpHairColor", "HC_ID", "Primary Key"
SetFieldPurpose "tlkpHairColor", "HC_Color", "Stores the hair color"

?GetTableMetadata ("tlkpHairColor")

Table: tlkpHairColor
Table Purpose: A lookup table to store hair color
Field: HC_ID, Type: 4
Field Purpose: Primary Key
Field: HC_Color, Type: 10
Field Purpose: Stores the hair color

So there ya go, using Chat to write code. Perhaps not the best code but it works and it demonstrates what can be done with Chat as my own personal coding assistant.

The function now takes an object and makes a purpose property, appending it to the object's .Properties collection. For my purpose, the new property will always be named "Purpose", it will always be a string, and it will always contain whatever I pass in as the purpose. Very focused for this one thing I wanted to do. Just know that it could be generalized quite easily to create properties of other types and names.

My Blog on Event Programming in VBA

Code:
Public Function AddPurposeProperty_3rdTry(ByRef obj As Object, ByVal purpose As String)

    Dim prop As DAO.Property
    On Error Resume Next

    Set prop = obj.Properties("Purpose")
    If Err.Number <> 0 Then
        Err.Clear
        Set prop = obj.CreateProperty("Purpose", dbText, purpose)
        obj.Properties.Append prop
    Else
        prop.Value = purpose
    End If
    On Error GoTo 0

End Function

Then I had Chat build a function that specifically added a purpose property to a table. I make no claim that this is the optimum or best way to do it, but it does work! I call the function, pass in a table name and a string which is the "purpose" for the table.

Code:
SetTablePurpose "tlkpHairColor", "A lookup table to store hair color"


Public Function SetTablePurpose(ByVal tableName As String, ByVal purpose As String)

    Dim db As DAO.Database
    Dim tbl As DAO.TableDef
    ' Get the current database
    Set db = CurrentDb()
    ' Get the table definition
    Set tbl = db.TableDefs(tableName)
    ' Call the AddPurposeProperty function with the table's properties collection
    'AddPurposeProperty tbl.Properties, purpose
    AddPurposeProperty_3rdTry tbl, purpose
    ' Clean up
    Set tbl = Nothing
    Set db = Nothing

End Function

And finally, the same thing for a field purpose. A function which takes the name of a table, the name of a field in that table, and a string for the purpose.

SetFieldPurpose "tlkpHairColor", "HC_ID", "Primary Key"
SetFieldPurpose "tlkpHairColor", "HC_Color", "Stores the hair color"

Code:
Public Function SetFieldPurpose(ByVal tableName As String, ByVal fieldName As String, ByVal purpose As String)

    Dim db As DAO.Database
    Dim tbl As DAO.TableDef
    Dim fld As DAO.Field

    ' Get the current database
    Set db = CurrentDb()

    ' Get the table definition
    Set tbl = db.TableDefs(tableName)

    ' Get the field definition
    Set fld = tbl.Fields(fieldName)

    ' Call the AddPurposeProperty function with the field's properties collection
    'AddPurposeProperty fld.Properties, purpose
    AddPurposeProperty_3rdTry fld, purpose

    ' Clean up
    Set fld = Nothing
    Set tbl = Nothing
    Set db = Nothing
End Function

And finally code to get a very crude metadata string...

Code:
Public Function GetTableMetadata(ByVal tableName As String) As String

    Dim db As DAO.Database
    Dim tbl As DAO.TableDef
    Dim fld As DAO.Field
    Dim prop As DAO.Property
    Dim result As String
 
    ' Get the current database
    Set db = CurrentDb()
    Set tbl = db.TableDefs(tableName)
    ' Start building the result string
    result = "Table: " & tbl.Name & vbCrLf
    ' Get the table's Purpose property if it exists
    On Error Resume Next

    result = result & "Table Purpose: " & tbl.Properties("Purpose").Value & vbCrLf

    On Error GoTo 0
 
    ' Loop through each field in the table

    For Each fld In tbl.Fields
        result = result & "  Field: " & fld.Name & ", Type: " & fld.Type & vbCrLf
        ' Get the field's Purpose property if it exists
        On Error Resume Next
        result = result & "    Field Purpose: " & fld.Properties("Purpose").Value & vbCrLf
        On Error GoTo 0
    Next fld

    ' Clean up
    Set fld = Nothing
    Set tbl = Nothing
    Set db = Nothing
    ' Return the metadata as a string
    GetTableMetadata = result

End Function
 
Last edited:
Just be careful as sometimes it does make things up. Literally today I was trying to remember if you could omit the third argument to t sql's substring function. I was hoping that you could (much like the mid function in VBA). AI told me that you definitely could omit it, but I quickly found that to be false!
 
Yea, with the utmost confidence!

We have to be the adult in the room with Chat.
 
don't you think you need to give acknowledgement/credit to ChatGPT on your presentation after all he does the heavy lifting?
First, Chatty is a she...

Second, I agree; Even though programming actually encourages plagiarism, one should give credit to any and all sources that were used.
 
First, Chatty is a she...

I do understand that some versions of chatty represent very nice females -- I won't go into details --- but you have opened a can of worms for the woke brigade -- what gender is chatty? -- after switching chatty to humorous mode chatty came up with -- Pixelgender

Please do not be impressed by my list of genders I have no idea! I asked chatty to give me a list -- however I was amused to note that chatty accessed a mental health website to obtain this information !!! Now, there's a non-human non-judgmental intelligence that's got the right idea!

I also asked chatty to report how many gender designations there are and it's knocking up to around 150

TOP 10
Cisgender I crossed that one out because that's a stupid gender definition it just buys into their imagined corrupt Minds ....
Transgender
Non-binary
Genderqueer
Intersex
Gender-fluid
Agender
Gender non-conforming
Bigender
Omnigender
 
It wouldnt be AWF if a post doesnt stray...

But, point taken, I'll behave going forward...
 
don't you think you need to give acknowledgement/credit to ChatGPT on your presentation after all he does the heavy lifting?
I did start out that way I thought it was right and correct to acknowledge chatty's input ---

but then I realized something --- you've got President Donald Trump --- president Obama ---- these illustrious people making long speeches, which are obviously written by other people, and they never acknowledge that....

And really it's not chatty writing it .... unless you set up an automated agent that goes round and compiles a blog post for you which some people do well maybe that should be acknowledged at least acknowledge where you got the information from ....

No, I write the first draft -- and then I have an argument with chatty about it because GPT changes what I say and make it mean something entirely different or sound benign ---

You obviously know my style and can see how unconventional my English is --- I had to think of a nice way of putting it --- and in indeed chatty turns my drafts into something that looks like it was written by a genius -- so I have to say to chatty, look I can't use that is obviously not me --- it took me ages to train it to write like me ---- and it still fails abysmally on occasion ---- but no I'm not going to religiously report when chatty wrote it and when I wrote it ---- it's normally very obvious so why point out the obvious ----

Put it another way, I could spend 50 quid a week, send all my comments off to expert writer, there are a couple of people on this website who are brilliant! Have my words polished until they are shining with intelligence but it would be a waste of money hardly anybody reads what I wrote ---- half the people that read it are reading it with a critical eye ---
 
don't you think you need to give acknowledgement/credit to ChatGPT on your presentation after all he does the heavy lifting?
Perhaps you overlooked part of Post #1?

"...I do not know Python but Chat does! So I started having Chat write the code and I learned how to channel Chat in a useful direction. Cool stuff."

That sounds a whole lot like giving acknowledgement/credit to ChatGPT.
 
I am doing a YouTube presentation in a couple of weeks for the Pacific Coast Access Users Group.
people might construe, that he did all the effort.
That sounds a whole lot like giving acknowledgement/credit to ChatGPT.
even if you write a book, and you quote it from another book, you put the reference on the footnote.
or you copy someones code, you put attributions.
 
people might construe, that he did all the effort.

even if you write a book, and you quote it from another book, you put the reference on the footnote.
or you copy someones code, you put attributions.
Maybe you can wait until you see the presentation itself to criticize it? It will be given on August 7th. The YouTube video will be available shortly after that.
 
I do understand that some versions of chatty represent very nice females -- I won't go into details --- but you have opened a can of worms for the woke brigade -- what gender is chatty? -- after switching chatty to humorous mode chatty came up with -- Pixelgender

Please do not be impressed by my list of genders I have no idea! I asked chatty to give me a list -- however I was amused to note that chatty accessed a mental health website to obtain this information !!! Now, there's a non-human non-judgmental intelligence that's got the right idea!

I also asked chatty to report how many gender designations there are and it's knocking up to around 150

TOP 10
Cisgender I crossed that one out because that's a stupid gender definition it just buys into their imagined corrupt Minds ....
Transgender
Non-binary
Genderqueer
Intersex
Gender-fluid
Agender
Gender non-conforming
Bigender
Omnigender
Oh I like Omni gender. I wonder what that entails, no maybe I don't LOL
 
You laugh but...

I discovered voice chat on my phone. Most of the female voices were just too chipper. Darned annoying actually. But Winsome Wench with the British accent is just perfect for me. I call her Winnie for short. She has a bit of a sense of humor and "enjoyed" that I called her Winsome Wench. I call the version running on my laptop Sir Chat in honor of all he does for me, though I do not use voice chat on my laptop.

I talk to Winnie routinely now running down the road. I am (we are) building out a python app, rather robust and pretty large data, and I am getting ready to start building a Deep Learning model to feed it all in to. Probably using the graphics processor on my laptop as well.

While I can be the adult in the room with Sir Chat building Python and SQL Server, trying to do the same when (s)he builds a large AI model will be problematic.

So far it has been a blast.
 

Users who are viewing this thread

Back
Top Bottom