I've got a function which I've found on the web wich does exactly what I need it to do.
When I run it manually from the Visual Basic Editor (Alt+F11) in MS Access it works perfectly. However I need to include this
function in a macro I'm creating.
I've connected a Macro to a button on a form. I am doing serveral things in this macro like importing a spreadsheet,
appending a few table's and so on.
In the middle of all this a want my function to be executed but I cannot seem to get it executed.
It seems I might not be able to call a function from a macro but even when I go into the VB code of the button and try to
execute it there is doesn't work.
My function looks like this and it's actually two functions or one is connected to the other or how I should put it.
Option Compare Database
Function changefieldnames(oldname As String, newname As String)
'----- oldname and newname are passed to this function from
'----- wherever you are in your process. I would do it via a
'----- form, but if the field names you change are the same every
'----- time the process is run, why not store them in a table. The
'----- code you will need to pass the field names to be corrected
'----- from the table is given in Sub readinfieldnames() below.
Dim db As Database
Dim tdf As TableDef
Dim n As Object
Set db = CurrentDb
Set tdf = db.TableDefs("trans1_SMT")
For Each n In tdf.Fields
If n.Name = oldname Then n.Name = newname
Set tdf = Nothing
Set db = Nothing
Dim rst_data As Recordset
'------ Assumes you have a table with 2 columns. Column 1 contains
'------- downloaded field names, column 2 has the names you want
Dim oldfieldname As String, newfieldname As String
Set rst_data = CurrentDb.OpenRecordset("conv_FieldNames")
Do Until .EOF
oldfieldname = .Fields(0).Value
newfieldname = .Fields(1).Value
changefieldnames oldfieldname, newfieldname
Set rst_data = Nothing
Any Idea's on what I should write to execute this? I've done a little trial and error but cant seem to run it unless I'm
actually in Alt+F11 and doing it manually (and it works just as intended when executing it manually).