MS Access correctly utilizing a string variable within DLookup - variables

I am attempting to replace the domain parameter of DLookup with a variable, the intent being a single place to make a change if one is required. This is how I am declaring the variable:
Dim MnMnuSettingTbl As String
MnMnuSettingTbl = "'tblMainMenu'"
This is the original segment where the variable is to be used:
Me.MainMenuChoiceOne.Caption = DLookup("BtnText", "tblMainMenu", "ID = 1")
I wish to replace the domain criteria "tblMainMenu" with the variable, but when I attempt to do so it either does not compile, or I get an error message stating the table can not be found. I have reviewed several articles on this matter, and I am gathering I am not passing the variable correctly, via the improper use of single or double quotes. I'm rather embarrassed, so at this point I am looking for the correct way to either format the variable or the correct way to use it within the DLookup context.

The variable must contain the same constant string as you currently have in the DLookup.
MnMnuSettingTbl = "tblMainMenu"
Me.MainMenuChoiceOne.Caption = DLookup("BtnText", MnMnuSettingTbl , "ID = 1")
Single quotes would be needed for string parameters in the WHERE clause, e.g.
strTextID = "'QD42'"
x = DLookup("foo", "bar", "TextID = " & strTextID)

Related

Passin SSRS parameters or dataset count to vb code?

I'm working in SSRS and have succeeded in creating a 5 tier cascaded parameter set up. Now I'm trying to say that if ALL is selected on tier 2 then filter the dataset differently than of single select.
TO do that I need count of selected and count of all and am trying to write some vb code in ssrs to fix this.
Pasted in this from microsoft
Public Function ShowParameterValues(ByVal parameter as Parameter)
as String
Dim s as String
If parameter.IsMultiValue then
s = "Multivalue: "
For i as integer = 0 to parameter.Count-1
s = s + CStr(parameter.Value(i)) + " "
Next
Else
s = "Single value: " + CStr(parameter.Value)
End If
Return s
End Function
But get error message:
The Value expression for the textrun 'Textbox4.Paragraphs[0].TextRuns[0]' contains an error: [BC30451] 'ShowParameterValues' is not declared. It may be inaccessible due to its protection level.
So 2 questions - 1 how do I declare it and what do I pass in? Options Are
Report.Parameters!Company
Parameters!Company
Parameters!Company.Value
Any help greatly appreciated
Pete
Two things here...
To solve your error, make sure you call the function in the expression like this
=Code.<myFunctionName>(parameters)
So in your case it would probably be
=Code.ShowParameterValues(Parameters!Company)
(you might need to use Parameters!Company.Value, I can't remember)
However, you said you wanted to just know if a single value multiple values have been selected. You can do this without custom code.
=Parameters!Company.Length
Will return the number of selected parameter values.
You can read more about this and other parameter related expressions here
https://learn.microsoft.com/en-us/sql/reporting-services/report-design/built-in-collections-parameters-collection-references-report-builder?view=sql-server-ver16

Access vba pass variable into code

I'm in Access 07 trying to pass a variable to a line of code. The purpose is to make an array of field names and then loop over the array performing the same action on each field. I've simplified it down to avoid any potential problems with the array or the loop.
In any other language I am familiar with it would be pretty simple, but I can't seem to format it in VB.
Me.FeildName.Locked = True
would be the static code, and I think the variable code would look something like this:
Dim Temp as String
Temp="FieldName"
Me.[Temp].Locked = True
but it keeps giving me an error that says "can't find the field '|' referred to in your expression", so it's not reading the value of the variable.
How do I get it to read the variable in the command?
Alternatively, I've tried to concatenate strings into a line of code:
Dim CodeLine As String
Dim TestName As String
TestName = "FieldName"
CodeLine = "Me.[" & TestName & "].Locked = True"
This creates a string that looks like functional code but how would I run it?
Thanks
If Me is a form, you need
Temp = "FieldName"
Me.Controls(Temp).Locked = True
Dick Kusleika's answer is the way to go, but for completeness' sake, you can do also something like this:
For i = LBound(strControlNames) To UBound(strControlNames)
CallByName Forms![MyFormName].Controls(strControlNames(i)), "Locked", VbLet, "True"
Next
strControlNames would be the array with your control names.

Using a global var in an SQL query

I have an access project in wich I use a global variable across all the forms to show what user is logged in.
Public activeUser As String
On one of the forms I want show data depending on who is logged in. there are several fields on the form, so I want to use a query as data source across the whole form.
Now here is my problem:
I tried to make a query that uses the value in activeUser to look up all the values that the active user should be able to see:
SELECT WBS_Id,
Verantw,
Commentaar
FROM tbl_New_WPE_User
WHERE UserName LIKE activeUser
Why does this not work? and How can I make it work?
One option would be to create a VBA function to return the value of the global variable. For example
Public Function GetActiveUser() As String
GetActiveUser = activeUser
End Function
Then you could use the function in a saved query like
SELECT WBS_Id,
Verantw,
Commentaar
FROM tbl_New_WPE_User
WHERE UserName LIKE GetActiveUser()
However, in this particular case I would be inclined to get rid of the global variable and just call the function to get the current user (i.e., the code you used to populate the global variable). I doubt that the extra overhead of retrieving the value each time (e.g., by getting the .Username property from a WScript.Network object) would amount to very much, and if it did turn out to be an issue then you could always consider using a Static variable inside your function to cache the value.
i guess SQL is written in VBA code in form of string:
Public activeUser As String
dim tmpSql As String
tmpSql = "SELECT WBS_Id, Verantw, Commentaar FROM tbl_New_WPE_User WHERE UserName LIKE activeUser"
if so, then you should do the following:
tmpSql = "SELECT WBS_Id, Verantw, Commentaar FROM tbl_New_WPE_User WHERE UserName LIKE " & activeUser & "*"
where * in Access is the equivalent for % in SQL Server
Now, if the SQL statement is written in an Access Query, then it's not possible to use VBA variable activeUser

Razor VB - Pass a Null value if a Request variable is empty

I've been trying so hard to find a solution for this, but no luck. I'm fairly new to VB and SQL, but this shouldn't be too hard. I've inherited a lot of code and there's not too much room to change db attribute types or anything.
I'm trying to run an UPDATE query using parameters in Razor, like so:
Dim updateCommand = "UPDATE [tblJobRating] SET [ProjectManagement] = #0, [ProjComments] = #1, [Schedule] = #2, [SchedComments] = #3 WHERE [JobRatingID] = #4"
All of the columns in question need INT values, but I have one exception where I need to pass it a null value (passing another number or zero won't do). Essentially a "N/A" value for the user.
I assign the variables from Post requests, like so:
Dim projMgmt = Request.Form('projMgmt')
' ...
Dim sched = Request.Form('sched')
I have the "N/A" value posting no value right now (or it can be a string and I can check for IsNumber if need be, I guess). But when I call the query execution, it enters the value as a 0.
db.Execute(updateCommand, projMgmt, projComments, sched, schedComments, ratingId)
It needs to be a NULL value for the backend to work properly. I've tried type checking and passing Nothing, System.Data.SqlTypes.SqlInt32.Null, etc., but it either gives conversion errors or sets to 0. How can I pass it properly?
Edit: I left out the first param in the db.Execute method, passing in the updateCommand. Edited for clarity.
The problem is in your vb variable definition. I assume you have an integer, it needs to be a nullable(of integer) all the way through to the SQL. This can also be written as integer?.

Error when trying to cast db query to string

I am trying to extract one integer from the database but am getting errors
My code in the controller is:
Dim SeqNumQuery = From a In db.RequestedServices
Where a.RequestedServiceId = id
Select a.StationId
Dim StationId As Integer = SeqNumQuery.ToString
And error is:
Range variable 'StationId' hides a variable in an enclosing block or a range variable previously defined in the query expression.
I think the issue is that your variable, StationId, is being declared somewhere already within the same scope. Try changing to:
Dim SeqNumQuery = From a In db.RequestedServices
Where a.RequestedServiceId = id
Select a.StationId
StationId = SeqNumQuery.ToString
But look into what the other variable is being used for. If you need it, use something other than StationId for your variable name, for example RequestedServicesStationId.
More information regarding this error:
Range variable hides a variable in an enclosing block or a range variable previously defined in the query expression.
On a side note, I don't understand why you are using .ToString extension method. This won't cause your script to throw an exception, however I would recommend changing this to CInt():
StationId = CInt(SeqNumQuery)