Pulling results from one function into another function - ms-access

I am using Access 2010. How do I get the information in a recordset from one function moved into a different function? The following code is giving me an object variable not set error:
Function Main()
Dim rs as DAO.RecordSet
Dim i as integer
Set rs = QueryResults(i)
Do While Not rs.EOF
debug.print rs!result
rs.MoveNext
Loop
End Function
Function QueryResults(i as Integer) as DAO.RecordSet
Dim stQuery as String
Dim rsResults as DAO.RecordSet
Dim i2 as Integer
i2 = i
stQuery = "Select query that uses i2 to get results"
Set rsResults = CurrentDb.OpenRecordset(stQuery)
End Function
I can step through and see that the QueryResults function is working. I can print out each line from rsResults and see that it has what I want. The problem comes when the code hits the "do while not rs.EOF" line. It says that rs was never set to anything.
What am I doing wrong? Both functions are in the same module. It seems like it shouldn't be that hard to get results from one function into another function.

When you want a function to return something, you must assign that something to the function name.
Change the last part of your QueryResults function to this:
'Set rsResults = CurrentDb.OpenRecordset(stQuery)
Set QueryResults = CurrentDb.OpenRecordset(stQuery)
End Function

Related

Setting Combobox Value With VBA MS Access

I am trying to make a module to pass a single result of a query into a Combobox to be populated/displayed immediately on Form_Load event, but I keep getting the following error: Run-time error '2465' "Microsoft Access can't find the field 'MyCombo' referred to in your expression"
Query result is tested and returning the proper value. the problem is in the reference call to the MyCombo combobox.
This is my code below:
Public Function getSetRefNo() As String
Dim rs1 As DAO.Recordset
Dim currentformName As String
currentformName = Screen.ActiveForm.Name
Dim docidrefnoquery As String
Dim dociddefaultvalue As String
Set rs1 = CurrentDb.OpenRecordset("SELECT DISTINCT ColA FROM TblA WHERE ColC = " & Forms(currentformName)![Combo25])
Do Until rs1.EOF = True
docidrefnoquery = rs1(0)
rs1.MoveNext
Loop
rs1.Close
Set rs1 = Nothing
dociddefaultvalue = DLookup("RefNo", docidrefnoquery) 'RefNo here is the target column in the Query
Forms(currentformName)![MyCombo] = dociddefaultvalue 'MyCombo here is the Combobox Name
Debug.Print docidrefnoquery & " - " & dociddefaultvalue
End Function
on the targeted form, I use this code:
Private Sub Form_Load()
Call getSetRefNo
End Sub
after opening the targeted form, I receive the above mentioned error. I don't know what's wrong I tried to trace everything and it seems to be fine, I used the same chunk of codes in other places and worked fine. don't know what's wrong here to be honest. I would be grateful if you could help me elaborate what's going on.
I had to alter the form_load event like the following:
Private Sub Form_Load()
Me.TimerInterval = 30
End Sub
Private Sub Form_Timer()
Me.TimerInterval = 0
Call getSetRefNo
End Sub

it is possible to call ms-Access function from outside

I created a public function in an existing Access form and I`m trying to call it from outside the application.Very simple function just created for testing this.
Public Function test1(ByVal test1 As String)
Dim xlApp As New Excel.Application
xlApp.Visible = False
Dim wb As Excel.Workbook
Set wb = xlApp.Workbooks.Add
Dim ws As Excel.Worksheet
Set ws = wb.Worksheets(1)
End Function
I created the connection to it, on Automation Anywhere and I´m trying to call the function created.
Connection String im using:
Provider=Microsoft.ACE.OLEDB.16.0;Data Source="$connection$";Jet OLEDB:Database Password="$pass$"
Tried doing this, without suceed
Select test1("test")
EXEC test1("test1")
EXECUTE test1("test1")
Also with simple '
No way to do this on background so as suggested below I created a sub and called it from an VB Script
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase "RUTA ACCESS",,"CONTRASEÑA"
appAccess.UserControl = True
appAccess.Run "generarEtiqueta","numPropuesta","numExp","fileSavePath"
appAccess.CloseCurrentDatabase
appAccess.Quit
generarEtiqueta is the sub, all the other are arguments
You can create a module in access vba to use it in any form, or independent of form. The function can be called from outside as such:
Dim appAccess As New Access.Application
appAccess.OpenCurrentDatabase ("C:\My Documents\myDatabase.mdb")
appAccess.Run "myDatabase.test1", "Pass your argument here"
Running code behind a form requires the form to be open.
Example of a VBScript running a Sub and Function procedures in Access general module as well as a macro.
Dim ObjAccess
Set ObjAccess = CreateObject("Access.application")
ObjAccess.visible = false
ObjAccess.OpenCurrentDatabase("filepath\filename.accdb")
ObjAccess.Run("FunctionName") 'not finding a way to pass argument to Function
ObjAccess.Run "SubName", "argument" 'if Sub does not require argument then eliminate
ObjAccess.DoCmd.RunMacro "MacroName"
ObjAccess.Quit
Set ObjAccess = Nothing
I tested calling a Sub that issued a MsgBox and a Debug.Print. MsgBox works, Debug.Print does not.

How to get the maximum value of the "Recordset" form?

Form structure:
- frm_00_00_MainForm;
- - frm_02_02_Groups_Tbl;
- - frm_reg_GroupsStud_Stud_IdGroup_tbl.
 
Form data source ([frm_reg_GroupsStud_Stud_IdGroup_tbl]) - request.
 
SELECT reg_GroupsStud_Stud. *, Reg_GroupsStud_Stud.id_group
FROM reg_GroupsStud_Stud
WHERE (((reg_GroupsStud_Stud.id_group) = [Forms]! [Frm_00_00_MainForm] [[id_group_frm]));
 
I need to get the maximum value of the [id_stud] field.
So that the code does not process all lines of the request, but only those that are in the form I think this can speed up the code.
For this, I create a "Recordset" form.
 
For this, I try to use the following code:
    Private Sub btnMaxValue_Click()
Dim rst As DAO.Recordset
Dim studMax As Integer
Set rst = Me.[frm_reg_GroupsStud_Stud_IdGroup_tbl].Recordset
studMax = rst.???
End Sub
But I do not understand how to get the maximum value of the [id_stud] field.
Question.
How to get the maximum value of the "Recordset" form?
You could try sorting the recordset, and then taking the last value. This code is run from the sub form.
Private Function GetMax() As Long
Dim rst As DAO.Recordset
Dim studMax As Long
Set rst = Me.RecordsetClone
rst.Sort = "Id_stud"
rst.MoveLast
studMax = rst("Id_stud")
GetMax = studMax
rst.Close
End Function
You need to add error handling and code to check if records are available in the record set. You can assign the value of the GetMax() function to any control or VBA logic, from within the sub form.

insert data to a record already saved to a table

I have a recordset that is missing one field for each record and would like to add some data from a form by looking up certain criteria. I used a select query to get data onto the form in the first place and tried to reverse the assigning of values but it doesnt work as it says Run-time error 3027 'the Database or Object is Read only.' I think this is because I ran a select query to get the information but how do I input the data to the same records. the code I used is below -
Private Sub CmdAppend_Click()
Dim dbsNorthwind As dao.Database
Dim rstAmend As dao.Recordset
Dim qdfAmend As dao.QueryDef
Dim n As Integer
Set dbsNorthwind = CurrentDb
Set qdfAmend = dbsNorthwind.QueryDefs("Get_Questions_NTL")
qdfAmend.Parameters(0) = [Forms]![TeamLeader]![ComClientNotFin]
qdfAmend.Parameters(1) = [Forms]![TeamLeader]![ComDateSelect]
Set rstAmend = qdfAmend.OpenRecordset(dbOpenDynaset)
n = 0
rstAmend.MoveFirst
Do Until rstAmend.EOF
n = n + 1
rstAmend.Fields("ManagerID") = Form.Controls("SC" & n).Value
rstAmend.MoveNext
Loop
End Sub
You'd have to use the .Edit and .Update methods of the recordset object to update records. You received the error because you are trying to assign a value to a read only property.

How can I get reference to a variable by using a string, in VBA?

I have a variable strFunction, then I have another string strName = "strFunction" , what I want to know is how can I get the value of strFunction by using strName.
For example, something like getValue(strName) gives me the value of strFunction. Is it possible in Access VBA?
Thanks!
EDIT:
I have a strFunction string, it's a const string.
In my code I want to use Len("strFunction") to test the length of it, but what i got is the length "strFunction". So I need a get-value-out-of-variable-name function. I have tried Eval(), but it cannot do this, even I write a get_strFunction(), eval("get_strFunction()") gives me error, telling me it cannot find it.
Private Const strFunction as String = "FilterByType_1"
Private Function get_strFunction()
get_strFunction = strFunction
End Function
"I have a variable strFunction, then I have another string strName = "strFunction" , what I want to know is how can I get the value of strFunction by using strName."
Instead of a variable, strFunction could be the key for an item in a VBA collection.
Public Sub darkjh()
Dim strName As String
Dim col As Collection
Set col = New Collection
col.Add "FilterByType_1", "strFunction"
strName = "strFunction"
Debug.Print col(strName)
Set col = Nothing
End Sub
Edit: Instead of a VBA collection, you could use a Scripting.Dictionary.
Dim strName As String
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "strFunction", "FilterByType_1"
strName = "strFunction"
Debug.Print dict(strName)
Set dict = Nothing
Option Compare Database
Dim a As String
Dim b As String
Public Sub test()
a = "b"
b = "test-string"
Debug.Print Eval("get" & a & "()")
End Sub
Public Function getB() As String
getB = b
End Function
Output
>>test
test-string
eval(a) did not work, so I had to write a "getter" for the variable and eval that function: eval("get" & a & "()").