#Name? when populating text box with stored query (1 Viewer)

Margarita

Registered User.
Local time
Today, 15:36
Joined
Aug 12, 2011
Messages
185
Hello,
This is a long question- please read to the end. I really need help!

Users want to see a simple form where for each vendor in the database, they can see the total budgeted amount, spent amount, and left over amount- per vendor, per budget code (BC), and per object code (OC).
They need the flexibility to set the budget code and object code filters or to leave them blank and just see the total for the vendor for the year.

The fiscal year is chosen from a listbox- FiscalYearlistbox, BC and OC typed by user in textboxes- OCtoSearch and OCtoSearch.

My goal is to display the Budgeted, Spent, and Left Over numbers in textboxes. Budgeted and Spent are to be populated from sql select statements.

Here is my try for Budgeted. In the OnCurrent event of the form, I create the sql statement with part 1 which is the main select, and part 2, which is additional where criteria if the user chooses to specify BC and/or OC. Then I set this 2-part string as the SQL statement of a placeholder query. Then I use dlookup on this query (returns single field with single record) to set the control source of the BudgetedSum textboxt. Or at least that's what I want to do- I am not successful. The value that I get in BudgetedSum is #Name? for all vendors.

For some vendors, there is no budgeted value- they weren't entered in the budget table at the beginning of the year, we just bought stuff from them later. For these vendors, I understand that there would be no value in BudgetedSum or an error value or whatever. But why shouldn't I get a legitimate value when I scroll to those vendors who do have a budgeted amount? The sql statement that I wrote does return the right values when I run it as a stored query. Why doesn't the the control source get set correctly?

Here is my code. I would be really grateful if someone could tell me if I am being blind to something completely obvious here.

PHP:
'part 1 of SQL statement: always filter by fiscal year
'----------------------------------------
Dim str_part1 As String
str_part1 = "select sum(budgetAmount) as SumBudgeted from (Select budgetAmount, fiscalyearend, budgetcode, objectcode from budgetedOTPS inner join " & _
"(Select servicesubcat, budgetcode from Services inner join budgetcodes  " & _
"on services.agreementtype= budgetcodes.agreementtype where budgetcodes.reportingcat " & _
"like 'OTHER DIRECT COSTS') as A on BudgetedOTPS.Servicesubcat= A.ServiceSubcat)  as B inner join " & _
"FYStartDate on B.FiscalYearEnd= FYStartDate.FyEnddate " & _
"where FYstartdate.FYname like '" & Me.FiscalYearlistbox.Value & "' "
'----------------------------------------

'part 2 of SQL statement: check if any if the other filters are not blank
'----------------------------------------
Dim str_part2 As String
str_part2 = ""
If Len(Me.OCtoSearch & vbNullString) > 0 Then
   str_part2 = " and B.ObjectCode like " & Chr(34) & Me.OCtoSearch & Chr(34) & " AND "
End If
 
If Len(Me.BCtoSearch & vbNullString) > 0 Then
   str_part2 = str_part2 & " and B.BudgetCode like " & Chr(34) & Me.BCtoSearch & Chr(34) & " AND "
End If
 
If Right(str_part2, 5) = " AND " Then
   str_part2 = Left(strWhere, Len(strWhere) - 5) & ";"
End If

If str_part2 = "" Or str_part2 = " AND " Then
   str_part2 = ";"
End If
'----------------------------------------
str_budgeted = str_part1 & str_part2    'this is the string that will replace the sql of placeholder query
Dim qdf_budgetedsum As DAO.QueryDef
Set qdf_budgetedsum = CurrentDb.QueryDefs!PURCHASING_budgeted_sum_placeholderquery
qdf_budgetedsum.SQL = str_budgeted

'set value of budgeted sum field
Me.BudgetedSum.ControlSource = DLookup("SumBUdgeted", "PURCHASING_budgeted_sum_placeholderquery")

Thank you.
 

mdlueck

Sr. Application Developer
Local time
Today, 15:36
Joined
Jun 23, 2011
Messages
2,631
For some vendors, there is no budgeted value- they weren't entered in the budget table at the beginning of the year, we just bought stuff from them later. For these vendors, I understand that there would be no value in BudgetedSum or an error value or whatever. But why shouldn't I get a legitimate value when I scroll to those vendors who do have a budgeted amount? The sql statement that I wrote does return the right values when I run it as a stored query. Why doesn't the the control source get set correctly?

I suspect you are getting bit by the same thing that caused me mental gymnastics to get my head around.

Database table columns are NULL'able unless you set the NOT NULL flag.

Field controls (text entry field) are also NULL'able.

But a numeric VBA variable type is NOT NULL'able!!!

I would suggest you use Nz() calls to protect VBA variables when touching (getting values from) the database object. Be sure to set the correct value to replace NULL with, perhaps either 0 in the case of numeric, vbNullString for strings.

I have when numeric fields really do need to be NULLable, I have coded a special "NULL indicator value" in the VBA layer. Example: -1 = NULL. I keep that in mind in my interface code between the table and VBA... the Nz() command I place -1 as the substitute value if NULL, and when placing values back into the table, I test for the NULL indicator value and to a quick either value else NULL indicator when I am setting values to go back into the table.

Oh, and I almost forgot this helpful post:

Safely read form field text controls
http://www.access-programmers.co.uk/forums/showthread.php?p=1131039#post1131115


To interface with form controls, I rolled my own bit of shared code to correctly handle touching text field controls.
 

Margarita

Registered User.
Local time
Today, 15:36
Joined
Aug 12, 2011
Messages
185
I suspect you are getting bit by the same thing that caused me mental gymnastics to get my head around.

Database table columns are NULL'able unless you set the NOT NULL flag.

Field controls (text entry field) are also NULL'able.

But a numeric VBA variable type is NOT NULL'able!!!

I would suggest you use Nz() calls to protect VBA variables when touching (getting values from) the database object. Be sure to set the correct value to replace NULL with, perhaps either 0 in the case of numeric, vbNullString for strings.

I have when numeric fields really do need to be NULLable, I have coded a special "NULL indicator value" in the VBA layer. Example: -1 = NULL. I keep that in mind in my interface code between the table and VBA... the Nz() command I place -1 as the substitute value if NULL, and when placing values back into the table, I test for the NULL indicator value and to a quick either value else NULL indicator when I am setting values to go back into the table.

Oh, and I almost forgot this helpful post:

Safely read form field text controls
http://www.access-programmers.co.uk/forums/showthread.php?p=1131039#post1131115


To interface with form controls, I rolled my own bit of shared code to correctly handle touching text field controls.


Hi Michael, thank you for taking the time to look at this.
I am sorry if I'm being dense, but I thought what you suggested was as simple as changing the first part of the sql statement to

PHP:
select nz(sum(budgetAmount), 0) as SumBudgeted from  ...etc

I thought that by doing that I would catch the instances where the sum may be null. This didn't improve my situation. I'm still seeing #Name? for all vendors.

I also tried creating a recordset off of qdf_budgetedsum and checking whether the recordset is empty or not. If it's not empty, then I set the control source of BudgetedSum to the Dlookup value. I thought by doing that I can catch the vendors that don't exist in the BudgetedOTPS table (and therefore, qdf_budgetedsum produces an empty recordset).
Still, no luck. I am seeing the same error value in the textbox.

Did I misunderstand your suggestion?

Thank you.
 

mdlueck

Sr. Application Developer
Local time
Today, 15:36
Joined
Jun 23, 2011
Messages
2,631

Margarita

Registered User.
Local time
Today, 15:36
Joined
Aug 12, 2011
Messages
185
Michael, unfortunately, your suggestion went way over my head. I read the reference post carefully, but I didn't see how I could apply it to my situation. I could try to use the same method of creating parameters instead of creating the sql with the two sub-strings. But I am not sure if that would solve me problem. The way I set up the sql to change in accordance with which filters the user specifies is already successfully working (in that the sql statement of qdf_budgetedsum is what I want it to be). I just can't see why the dlookup part isn't putting the right value in the textbox.

In any case, thank you Michael for helping me out. I have spent many hours trying to get this to work without any luck, so I am giving up on the textbox idea and going to try a subform instead. So instead of using Dlookup on the Placeholder query, I am going to change the query slightly to include the fields Vendorcode, budgetcode, and objectcode, and create a small linked subform with basically just the BudgetedSum field visible so it looks as much like a textbox as possible. I wanted to try the textbox because it seemed simpler and more elegant, but I guess I can't take it that last step of the way.
I will post back if I have success with the subform thing.

Thank you.
 

mdlueck

Sr. Application Developer
Local time
Today, 15:36
Joined
Jun 23, 2011
Messages
2,631
I read the reference post carefully, but I didn't see how I could apply it to my situation.

I meant for to see where Nz() was being utilized.

Not in the building of the SELECT SQL line.

Instead to protect VBA variables from incorrectly encountering NULL values that the DB object is capable of handling.

That was a SELECT query, same as what you are attempting to do, utilizing Nz() at the proper place to use it.

Better?
 

Margarita

Registered User.
Local time
Today, 15:36
Joined
Aug 12, 2011
Messages
185
Hi Micheal, yesterday I was completely braindead so the whole concept of setting up a command was completely beyond me. This morning I came in with a clear head and decided to look at your code one more time and it actually clicked. I just tried it and it works out! Thank you very, very much! I learned something very useful and important from your code.
Here is how I have it working now:

PHP:
'part 1 of SQL statement: always filter by fiscal year
'----------------------------------------
Dim str_part1 As String
str_part1 = "select nz(sum(budgetAmount), 0) as SumBudgeted from (Select vendorcode, vendorname, budgetAmount, fiscalyearend, budgetcode, objectcode from budgetedOTPS inner join " & _
"(Select servicesubcat, budgetcode from Services inner join budgetcodes  " & _
"on services.agreementtype= budgetcodes.agreementtype where budgetcodes.reportingcat " & _
"like 'OTHER DIRECT COSTS') as A on BudgetedOTPS.Servicesubcat= A.ServiceSubcat)  as B inner join " & _
"FYStartDate on B.FiscalYearEnd= FYStartDate.FyEnddate " & _
"where FYstartdate.FYname like '" & Me.FiscalYearlistbox.Value & "' and " & _
"B.vendorname like '" & Me.VendorName_Wildcard & "' and B.vendorcode like '" & Me.vendorcode & "'"
'----------------------------------------
 
 
'part 2 of SQL statement: check if any if the other filters are not blank
'----------------------------------------
Dim str_part2 As String
str_part2 = ""
 
If Len(Me.OCtoSearch & vbNullString) > 0 Then
   str_part2 = " and B.ObjectCode like " & Chr(34) & Me.OCtoSearch & Chr(34) & " AND "
End If
 
If Len(Me.BCtoSearch & vbNullString) > 0 Then
   str_part2 = str_part2 & " and B.BudgetCode like " & Chr(34) & Me.BCtoSearch & Chr(34) & " AND "
End If
 
If Right(str_part2, 5) = " AND " Then
   str_part2 = Left(strWhere, Len(strWhere) - 5) & ";"
End If
 
 
If str_part2 = "" Or str_part2 = " AND " Then
   str_part2 = ";"
End If
'----------------------------------------
 
str_budgeted = str_part1 & str_part2    'this is the string that will replace the sql of placeholder query
 
 
 
'Define attachment to database table specifics
  Set adoCMD = New ADODB.Command
  With adoCMD
    .ActiveConnection = CurrentProject.Connection
    .CommandType = adCmdText
    .CommandText = str_budgeted
    Set adoRS = .Execute()
  End With
 
  With adoRS
    'Was no record found?
    If .BOF Or .EOF Then
     ' Me.Clear
    '  LocateByPartNumber = False
    Else
      'Fetch the value found
      Me.BudgetedSum = Nz(adoRS!SumBudgeted, 0)
    End If
 

mdlueck

Sr. Application Developer
Local time
Today, 15:36
Joined
Jun 23, 2011
Messages
2,631
look at your code one more time and it actually clicked. I just tried it and it works out!

Excellent! ;)

One question... this bit of code...

PHP:
'part 1 of SQL statement: always filter by fiscal year
'----------------------------------------
Dim str_part1 As String
str_part1 = "select nz(sum(budgetAmount), 0) as SumBudgeted from ...

I doubt you need the Nz() command in the SELECT... or did you run into the case that your table column is not a required field, so might be NULL value?
 

Margarita

Registered User.
Local time
Today, 15:36
Joined
Aug 12, 2011
Messages
185
Excellent! ;)

One question... this bit of code...


I doubt you need the Nz() command in the SELECT... or did you run into the case that your table column is not a required field, so might be NULL value?


What we've had happen is when we're creating the budget, we may not know just how much to set aside for the vendor for that object code and we create the line in the table and then come back to add the amount when the amount of money avaialable is verified. So it's 'required' in that the creator of the budget should always fill in all the holes but since it's a flexible process, the field itself is best left as not required. That's why I thought I should look out for Nulls.

Thank you.
 

Users who are viewing this thread

Top Bottom