Store value in variable by using DoCmd Object in MS access - ms-access

Dear Expert I am trying to store value in variable by using DoCmd Object, but i am getting error that is Expected Function or Variable.
Please find my below VBA code.
Set chk = DoCmd.FindRecord("sagar", acAnywhere, True, acSearchAll, False, acAll, True)

FindRecord doesn't return any values, you cannot assign results to variable. Not clear what does it mean chk in your code, but if you execute DoCmd.FindRecord first, this will move current record to the target row, then you'll be able to get/set values in record columns, delete the row or continue to search.

Related

VBA ADODB recordset field is Null from MySQL - is there an easy way to check or prevent "NULL"?

I pull a large recordset from a MySQL server and I assign a bunch of variables in an Excel VBA routine based on the field position, e.g.:
x = MyRecordset.Fields(0).Value
y = MyRecordset.Fields(1).Value
' etc...
The only time this throws an error is when one of the values is NULL, because VBA doesn't allow a string to be NULL. I have a question for both sides, VBA and the MySQL side in case the VBA side falls down:
VBA: I'm currently using On Error Resume Next to skip the NULL error - is there a better / more official way to achieve this? Is this error skipping slowing my code down?
MySQL: Is there a way to prevent "NULL" being sent, e.g. to quickly replace instances of NULL with an empty string in my SELECT statement?
If I remember correctly, if you have a variable that might hold null, concatenating an empty string will turn the variable into an empty string, while having no affect on any other value. But that might not work with ADO recordsets
x = MyRecordset.Fields(0).Value & ""
How to do that:
1 - on the VBA side
If I am not wrong, there is already a VBA function that does the job of converting nulls to default values. In your case that would then be
Nz(MyRecordset.Fields(1).Value,"")
Returning a zero length string if the field value is null.
2 - on the MYSQL side
You could use the COALESCE function, that sneds back the first non-null value in its parameters list. Your select will then look like:
SELECT COALESCE(myFieldName,'')
Use a function like that
Option Explicit
Function rcdString(rcdField As Variant) As String
If IsNull(rcdField) Then
rcdString = ""
Else
rcdString = rcdField.Value
End If
End Function
And
x = rcdString(MyRecordset.Fields(0))

MS Access Vb How to build a text string to reference a dynamic column in and Vb SQL query and then execute that text string using the Eval function

In the following example where POOPTrst is a a DAO recordset (systems object) referencing the Vb SQL query or statement.  this code returns "Run-time error 2482" and/or "Microsoft Access cannot find the name 'POOPTrst' you entered in the expression"  (note:  the POOPTVal variable holds a date).
POOPTWkDmd = Eval("POOPTrst" & "!" & POOPTVal)
In the following example where POOPTrst is a a DAO recordset referencing the Vb SQL query.  this code returns "Run-time error 3256" and/or "Item not found in this collection"  (note: with or without parenthesis around the variable POOPTVal)
POOPTWkDmd(POOPTCounterInt) = POOPTrst!Eval(POOPTVal)
If I remove the Eval function and the POOPT date variable and type literal characters into the code I get the proper/expected return value and/or response (note:  the problem is that 1/5/2009 is a dynamic value that is calculated at run time
POOPTWkDmd(POOPTCounterInt) = POOPTrst![1/5/2009]   
Use this syntax: rs("Fieldname") instead of rs!Fieldname
POOPTVal = "1/5/2009"
POOPTWkDmd = POOPTrst(POOPTVal)
for more explanation see this answer: https://stackoverflow.com/a/34969410/3820271
One issue is that Eval() doesn't know anything about VBA variables or objects such as recordsets. If you want to use those, build a string containing their values and give Eval() that string.
However, I'm not sure Eval() is what you should use here. It seems you want to reference the value of a field in your POOPTrst recordset, with a variable to hold that field name. If that is correct, use the variable with the recordset's Fields collection: POOPTrst.Fields(POOPTVal).Value

MS Access VBA: What does OpenRecordSet return?

A very simple question to those who have worked with VBA for Access before.
Set S = CurrentDb.OpenRecordSet("select COLUMN from TABLE")
Is S now an array of all the values that are in COLUMN?
If not, how can I otherwise use a For Each loop for those values that are in S ?
Did you look at the documentation?
Do While Not S.EOF
value = S!COLUMN
S.MoveNext
Loop
CurrentDb.OpenRecordSet returns an Object of Type "Recordset"
And this object has several methods, e.g EOF, MoveNext, Fields which you can use to iterate over the rows.

How to set a default value in an Access 2000 combobox

I have an Access 2000 form that contains a combobox. The combobox is bound to a field in a table. When the value in the table is null, I want to set a default value on combobox without making the record dirty. Setting defaultValue does not work unless it is a new record. When I try to set the value, I get an error "You cannot assign a value to this object".
Any thoughts?
Me.cboName.Value = Me!cboName.Value ' This causes the error mentioned above
Me.cboName.DefaultValue = Me!cboName.Value 'This does nothing on an existing record.
The DefaultValue is entered when a NEW RECORD is created. To have the value display for an existing record... the easiest way I can think to accomplish this is to usean unbound control. For example, if the field you are using is theName in the Current event you'd use code like this:
Private Sub Form_Current()
me.cboName.value = Nz(me.theName.value,defaultValue)
End Sub
Where defaultValue is your previously determined default value. This will effectively require you to have two controls for the name... One with the bound value and one with the displayed value. If you do this, you'll have to also add code to update theName, when you change cboName.
As Remou suggested, you should ask yourself if this is really what you want to do, as it is definitely at least a little messy.

getting minimum value in a recordset field without Looping

I am using Access 2007 and writing a macro in VBA.
"Analyze" subroutine is passed a recordset rev_rec. This recordset has two fields "PeriodNo" (Integer) and "Revenue"(double).
Minrev is a variable where I have to store the minimum value of "Revenue" field. The code that I am using is below.
Public Sub Analyze(rev_rec As DAO.Recordset)
Dim Minrev As Double
rev_rec.MoveFirst
Minrev = rev_rec("Revenue")
While Not rev_rec.EOF
If rev_rec("Revenue") < Minrev Then
Minrev = rev_rec("Revenue")
End If
rev_rec.MoveNext
Wend
rev_rec.MoveFirst
.
.
.
End Sub
I tried using DMin() with recordset but am not able to find a way to do that. Is there a way to do this without using any kind of loop?
I think your best bet is to build a new recordset using a SQL statement that only retrieves one record, the one with the minimum for the desired period. Another option is, you could open this particular recordset with the Order By on the Revenue column Ascending. This way you would know that the smallest value will be in the first record.
Andy Brown's suggestion of using DMin should also work. It's actually an idea very similar to my first suggestion.
The problem is that you're passing a recordset. If you just had the table or query name (or SQL statement), you could just use DMIN. Eg:
MinRev = DMIN("Revenue","TableOrQueryNameInQuotes","")
The third argument can be used to set some criteria. For example:
MinRev = DMIN("Revenue","TableOrQueryNameInQuotes","PeriodNo > 5")
Be warned, however, that the functions starting with D (DMIN, DLOOKUP, DSUM) run very slowly, although if you've got less than about 10,000 records you won't notice this.