473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

VBA Function output

thelonelyghost
109 100+
Basic Information
Software: Microsoft Access 2000
OS: Microsoft Windows XP Professional SP3

I've been trying to make some of the behind-the-scenes coding for one of my forms available in a module such as creating a query from certain inputs:
Expand|Select|Wrap|Line Numbers
  1. Function EqualsAttachAnd(sField As String, sValue As String, strDelim As String)
  2.  
  3.   If sValue = "''" Or sValue = "" Then Exit Function
  4.   Else
  5.     sValue = Trim(sValue)
  6.     strSQLContent = strSQLContent & strDelim & sField & "= '" & sValue & "'"
  7.   End If
  8.  
  9. End Function
Where strDelim is AND or OR. The problem is that it currently outputs to a string strSQLContent that is not declared in the module I have this library of functions defined in. It is, however, defined in the individual form. Is there some way for this function to just return the variable strSQLContent from the library as a string. Example:
Expand|Select|Wrap|Line Numbers
  1. Call lib.EqualsAttachAnd("Customer",Me.txtCustomer," AND ")
  2.  
  3. 'returns this string:
  4. " AND [Customer] = 'Microsoft'"

So far I've gotten that I should append the function declaration with As String but is that really all it takes? I have no way to test the code since I'm working on only one part of it at the moment. Of the few results I've found, I came across THIS which only served to confuse me.

How would I change the code I have? I'm already familiar with Java and how to do it like that, but I'm just beginning with VBA. In Java you simply put Return variable, in this case strSQLContent.
Jun 28 '10 #1

✓ answered by patjones

The typical usage for a function is to assign the function the value. For example:

Expand|Select|Wrap|Line Numbers
  1. Private Function GetSum(lngA As Long, lngB As Long) As Long
  2.    GetSum = lngA + lngB
  3. End Function

And when you call the function:

Expand|Select|Wrap|Line Numbers
  1. Dim lngSum As Long
  2. lngSum = GetSum(5.6, 3.4)
  3.  

I made my function Private, meaning it is accessible only in the module in which I defined it. You can use the Public keyword instead if you want other modules to have access to it.

Pat

17 6203
patjones
931 Expert 512MB
The typical usage for a function is to assign the function the value. For example:

Expand|Select|Wrap|Line Numbers
  1. Private Function GetSum(lngA As Long, lngB As Long) As Long
  2.    GetSum = lngA + lngB
  3. End Function

And when you call the function:

Expand|Select|Wrap|Line Numbers
  1. Dim lngSum As Long
  2. lngSum = GetSum(5.6, 3.4)
  3.  

I made my function Private, meaning it is accessible only in the module in which I defined it. You can use the Public keyword instead if you want other modules to have access to it.

Pat
Jun 28 '10 #2
thelonelyghost
109 100+
So all I would need to do is change my code to:
Expand|Select|Wrap|Line Numbers
  1. Function EqualsAttachAnd(sField As String, sValue As String, strDelim As String) As String
  2.  
  3.   If sValue = "''" Or sValue = "" Then Exit Function 
  4.   Else 
  5.     sValue = Trim(sValue) 
  6.     EqualsAttachAnd = strDelim & sField & "= '" & sValue & "'" 
  7.   End If 
  8.  
  9. End Function
Correct me if I'm wrong but this means I should recode my call statement:
Expand|Select|Wrap|Line Numbers
  1. 'Before:
  2. Call lib.EqualsAttachAnd("Customer",Me.txtCustomer," AND ")
  3.  
  4. 'After:
  5. strSQLContent = strSQLContent & lib.EqualsAttachAnd("Customer",Me.txtCustomer," AND ")
This should work?
Jun 28 '10 #3
patjones
931 Expert 512MB
I do believe that should work. Are you doing this in a class module, and hence the reason for the "lib" part of the call? Let me know if it works.

Pat
Jun 28 '10 #4
thelonelyghost
109 100+
@zepphead80
I just used your suggestion and it doesn't seem to be working. I'm not sure if it's because what you said didn't fit the situation or if another error is masking it. I get an error message saying:

The expression On Click you entered as the event property setting produced the following error:
Invalid outside procedure.

*The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
*There may have been an error evaluating the function, event, or macro.

It couldn't be more vague. I did use the [Event Procedure] selection for the On Click event, which I then put this code into it:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnSearch_Click()
  2.  
  3. 'Call SQL SELECT-statement to subform
  4. Me.frmTEMP2.Form.RecordSource = BuildSQLStr
  5. 'DoCmd.RunSQL
  6.  
  7. End Sub
The relevant code attached to this is:
Expand|Select|Wrap|Line Numbers
  1. Dim strDelim As String
  2.   strDelim = " AND "
  3. Dim strSQLContent As String
  4.   SQLContent = ""
  5.  
  6.  
  7. ...
  8.  
  9.  
  10. Public Function BuildSQLStr()
  11.  
  12. 'Setting Form inputs to variables for easier reference, trimming space characters from      '
  13. 'before/after string, and Setting all empty inputs to zero-length instead of null.          '
  14. SQLContent = lib.EqualsAttachAnd("OldLogNumber", Me.txtOldLogNumber.Value, strDelim) & lib.EqualsAttachAnd("CTSLogNumber", Me.intCTSLogNumber.Value, strDelim) & lib.LikeAttachAnd("ReportSentTo", Me.txtReportSentTo.Value, strDelim) & lib.EqualsAttachAnd("CustomerPartNumber", Me.txtCustomerPartNumber.Value, strDelim) & lib.GreaterThanAttachAnd("DateReceived", Me.dtMinDateR.Value, strDelim) & lib.LessThanAttachAnd("DateReceived", Me.dtMaxDateR.Value, strDelim) & lib.GreaterThanAttachAnd("CompletionDate", Me.dtMinDateClosed.Value, strDelim) & lib.LessThanAttachAnd("CompletionDate", Me.dtMaxDateClosed.Value, strDelim) & lib.LikeAttachAnd("PersonAssigned", Me.txtPersonAssigned.Value, strDelim) & lib.LikeAttachAnd("CTSAssemblyPlant", Me.txtCTSAssemblyPlant.Value, strDelim) & lib.EqualsAttachAnd("CustomerPartTracker", Me.txtCustomerPartTracker.Value, strDelim)
  15. SQLContent = SQLContent & lib.EqualsAttachAnd("PartNumber", Me.txtPartNumber.Value, strDelim) & lib.EqualsAttachAnd("ReturnType", Me.txtReturnType.Value, strDelim) & lib.EqualsAttachAnd("ProblemType", Me.intProblemType.Value, strDelim)
  16. SQLContent = SQLContent & lib.EqualsAttachAnd("SensorType", Me.cmbSensorType.Value, strDelim) & lib.EqualsAttachAnd("Series", Me.txtSeries.Value, strDelim) & lib.LikeAttachAnd("FaultCode", "* " & Me.txtFaultCode.Value, strDelim) & lib.EqualsAttachAnd("DateCode", Me.txtDateCode.Value, strDelim) & lib.EqualsAttachAnd("Make", Me.intMake.Value, strDelim) & lib.EqualsAttachAnd("Model", Me.intModel.Value, strDelim) & lib.GreaterThanAttachAnd("ModelYear", Me.txtMinModelYear.Value, strDelim) & lib.LessThanAttachAnd("ModelYear", Me.txtMaxModelYear.Value, strDelim) & lib.EqualsAttachAnd("Engine", Me.cmbEngine.Value, strDelim) & lib.EqualsAttachAnd("VIN", Me.txtVIN.Value, strDelim) & lib.GreaterThanAttachAnd("Mileage", Me.intMinMileage.Value, strDelim) & lib.LessThanAttachAnd("Mileage", Me.intMaxMileage.Value, strDelim) & lib.EqualsAttachAnd("MileageType", Me.cmbMileageType.Value, strDelim)
  17. SQLContent = SQLContent & lib.LikeAttachAnd("FailureMode/Complaint-Detailed", Me.memoFailureModeComplaintDetailed.Value, strDelim) & lib.LikeAttachAnd("FailureMode/Complaint-Simple", Me.txtFailureModeComplaintSimple.Value, strDelim) & lib.LikeAttachAnd("CTSAnalysis", Me.memoCTSAnalysis.Value, strDelim) & lib.LikeAttachAnd("CTSFindings", Me.memoCTSFindings.Value, strDelim) & lib.LikeAttachAnd("Comments", Me.memoComments.Value, strDelim) & lib.EqualsAttachAnd("BOMNumber", Me.txtBOMNumber.Value, strDelim) & lib.EqualsAttachAnd("CustomerLocation", Me.cmbCustomerLocation.Value, strDelim) & lib.LikeAttachAnd("Customer", Me.txtCustomer.Value, strDelim) & lib.EqualsAttachAnd("CorrectiveActionNumber", Me.txtCorrectiveActionNumber.Value, strDelim) & lib.LikeAttachAnd("MiscIDNumber", "* " & Me.memoMiscIDNumber.Value, strDelim) & lib.LikeAttachAnd("Misc", "* " & Me.memoMisc.Value, strDelim)
  18.  
  19. 'Clean up the concatenation of all of the fields into a SQL statement
  20. strSQLContent = Replace(strSQLContent, strDelim, "", 0, 1)
  21.  
  22. 'Set results to SELECT statement that can be of use
  23. BuildSQLStr = "SELECT * FROM [Return Log] WHERE " & SQLContent & " ORDER BY [Return Log].[" & Me.cmbGroupBy.Value & "]"
  24.  
  25. End Function
From [lib] Class Module:
Expand|Select|Wrap|Line Numbers
  1. Public Function EqualsAttachAnd(strField As String, strValue As String, strDelim As String) As String
  2.  
  3.   If strValue = "''" Or strValue = "" Then Exit Function
  4.   Else
  5.     strValue = Trim(strValue)
  6.     strSQLContent = strSQLContent & strDelim & strField & "= '" & strValue & "'"
  7.   End If
  8.  
  9. End Function
Expand|Select|Wrap|Line Numbers
  1. Public Function GreaterThanAttachAnd(strField As String, strValue As String, strDelim As String) As String
  2.  
  3.   If strValue = "''" Or strValue = "" Then Exit Function
  4.   Else
  5.     strValue = Trim(strValue)
  6.     strSQLContent = strSQLContent & strDelim & strField & ">= '" & strValue & "'"
  7.   End If
  8.  
  9. End Function
Expand|Select|Wrap|Line Numbers
  1. Public Function LessThanAttachAnd(strField As String, strValue As String, strDelim As String) As String
  2.  
  3.   If strValue = "''" Or strValue = "" Then Exit Function
  4.   Else
  5.     strValue = Trim(strValue)
  6.     strSQLContent = strSQLContent & strDelim & strField & "<= '" & strValue & "'"
  7.   End If
  8.  
  9. End Function
Expand|Select|Wrap|Line Numbers
  1. Public Function LikeAttachAnd(strField As String, strValue As String, strDelim As String) As String
  2.  
  3.   If strValue = "''" Or strValue = "" Then Exit Function
  4.   Else
  5.     strValue = Trim(strValue)
  6.     strSQLContent = strSQLContent & strDelim & strField & " LIKE '" & strValue & "*'"
  7.   End If
  8.  
  9. End Function
I'm not sure if it's just an error in my code or if it's some sort of permissions ritual I forgot to go through. Any ideas? Until I can get a more substantial error message I don't have any clue what to do.

EDIT: I tried another function in [lib] called ClearForm() and it fails as well, leading me to believe it's an issue with permissions. Is that correct?
Jun 28 '10 #5
gershwyn
122 100+
I believe the error message is complaining about these lines:
Expand|Select|Wrap|Line Numbers
  1. Dim strDelim As String
  2.   strDelim = " AND "
  3. Dim strSQLContent As String
  4.   SQLContent = ""
You can't assign a value to a variable like that, outside of a function or sub-routine. If strDelim is a value that you use often throughout the code and won't change, you can declare it as constant (use the Public keyword instead if you need to access it outside this module):

Expand|Select|Wrap|Line Numbers
  1. Private Const strDelim as String = " AND "
Setting strSQLContent to an empty string isn't necessary in VBA. It's going to start out that way anyway the first time you use it in a given scope. Personally, I think it's a good habit to be in, but you'd need to do it at the beginning of each sub/function.
Jun 28 '10 #6
patjones
931 Expert 512MB
Well one of the things that I see immediately, which alone may or may not fix the situation, but should be corrected, is that you are not assigning the results of the function calls properly. For instance, you should have:

Expand|Select|Wrap|Line Numbers
  1. Public Function EqualsAttachAnd(strField As String, strValue As String, strDelim As String) As String
  2.  
  3.   If strValue = "''" Or strValue = "" Then Exit Function
  4.   Else
  5.     strValue = Trim(strValue)
  6.     EqualsAttachAnd = strDelim & strField & "= '" & strValue & "'"
  7.   End If
  8.  
  9. End Function

If you don't have EqualsAttachAnd = somewhere in the function definition, the function will exit with nothing assigned to it, and then trying to reference the function as though it has a value could produce an error.

Now, for the overall picture...I don't have a problem with putting functions in class modules and calling them in the manner that you are. In this situation I don't think it's quite what you want to do, even if it does end up working eventually.

Usually in Access when we have a bound form and want to use code to set it's recordsource after something like a button click occurring, we'd do something like:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_Click()
  2.  
  3. Dim strSQL As String
  4. strSQL = "SELECT some_fields FROM some_table WHERE some_condition = TRUE"
  5.  
  6. Me.Recordsource = strSQL
  7.  
  8. End Sub

The logic of this could be more complicated depending on your specific situation, but generally this is all you need. Yes, you will get some unwieldy looking SQL, but in my opinion that's just the nature of it. Another way that this is often approached is to define the form's recordsource without the WHERE clause of the SQL:

Expand|Select|Wrap|Line Numbers
  1. ...
  2.  
  3. strSQL = "SELECT some_fields FROM some_table"
  4. Me.Recordsource = strSQL
  5.  
  6. ...

and then later on (after some event has occurred on the form, say) apply the filter which restricts the recordsource to a certain subset of records:

Expand|Select|Wrap|Line Numbers
  1. ...
  2.  
  3. Dim strFilter As String
  4. strFilter = "some_field = some_value"
  5. Me.Filter = strFilter
  6. Me.FilterOn = True
  7.  
  8. ...

It is basically the WHERE clause without the WHERE. For example, if I were trying to restrict my form's records to only those employees who work in the department I select from a combo box on the form:

Expand|Select|Wrap|Line Numbers
  1. strFilter = "empl_dept = " & Me.cboDeptSelected

So, you don't need to do it using the filter method, but at the very least I'd suggest that you simplify everything to the three lines that I laid out at the beginning of the post.

Pat
Jun 28 '10 #7
thelonelyghost
109 100+
Dear Lord!! I'm still having issues with this code but this time it's not liking the use of Null in the concatenation of the EqualsAttachAnd, etc.
Expand|Select|Wrap|Line Numbers
  1. Public Function BuildSQLStr()
  2.  
  3. Dim strSQLContent As String
  4.  
  5. 'Setting Form inputs to variables for easier reference, trimming space characters from      '
  6. 'before/after string, and Setting all empty inputs to zero-length instead of null.          '
  7. strSQLContent = lib.EqualsAttachAnd("OldLogNumber", Me.txtOldLogNumber.Value, strDelim)
  8. strSQLContent = strSQLContent & lib.EqualsAttachAnd("CTSLogNumber", Me.intCTSLogNumber.Value, strDelim)
  9. strSQLContent = strSQLContent & lib.LikeAttachAnd("ReportSentTo", Me.txtReportSentTo.Value, strDelim)
  10. strSQLContent = strSQLContent & lib.EqualsAttachAnd("CustomerPartNumber", Me.txtCustomerPartNumber.Value, strDelim)
  11. strSQLContent = strSQLContent & lib.GreaterThanAttachAnd("DateReceived", Me.dtMinDateR.Value, strDelim)
  12. strSQLContent = strSQLContent & lib.LessThanAttachAnd("DateReceived", Me.dtMaxDateR.Value, strDelim)
  13. strSQLContent = strSQLContent & lib.GreaterThanAttachAnd("CompletionDate", Me.dtMinDateClosed.Value, strDelim)
  14. strSQLContent = strSQLContent & lib.LessThanAttachAnd("CompletionDate", Me.dtMaxDateClosed.Value, strDelim)
  15. strSQLContent = strSQLContent & lib.LikeAttachAnd("PersonAssigned", Me.txtPersonAssigned.Value, strDelim)
  16. strSQLContent = strSQLContent & lib.LikeAttachAnd("CTSAssemblyPlant", Me.txtCTSAssemblyPlant.Value, strDelim)
  17. strSQLContent = strSQLContent & lib.EqualsAttachAnd("CustomerPartTracker", Me.txtCustomerPartTracker.Value, strDelim)
  18. strSQLContent = strSQLContent & lib.EqualsAttachAnd("PartNumber", Me.txtPartNumber.Value, strDelim)
  19. strSQLContent = strSQLContent & lib.EqualsAttachAnd("ReturnType", Me.txtReturnType.Value, strDelim)
  20. strSQLContent = strSQLContent & lib.EqualsAttachAnd("ProblemType", Me.intProblemType.Value, strDelim)
  21. strSQLContent = strSQLContent & lib.EqualsAttachAnd("SensorType", Me.cmbSensorType.Value, strDelim)
  22. strSQLContent = strSQLContent & lib.EqualsAttachAnd("Series", Me.txtSeries.Value, strDelim)
  23. strSQLContent = strSQLContent & lib.LikeAttachAnd("FaultCode", "* " & Me.txtFaultCode.Value, strDelim)
  24. strSQLContent = strSQLContent & lib.EqualsAttachAnd("DateCode", Me.txtDateCode.Value, strDelim)
  25. strSQLContent = strSQLContent & lib.EqualsAttachAnd("Make", Me.intMake.Value, strDelim)
  26. strSQLContent = strSQLContent & lib.EqualsAttachAnd("Model", Me.intModel.Value, strDelim)
  27. strSQLContent = strSQLContent & lib.GreaterThanAttachAnd("ModelYear", Me.txtMinModelYear.Value, strDelim)
  28. strSQLContent = strSQLContent & lib.LessThanAttachAnd("ModelYear", Me.txtMaxModelYear.Value, strDelim)
  29. strSQLContent = strSQLContent & lib.EqualsAttachAnd("Engine", Me.cmbEngine.Value, strDelim)
  30. strSQLContent = strSQLContent & lib.EqualsAttachAnd("VIN", Me.txtVIN.Value, strDelim)
  31. strSQLContent = strSQLContent & lib.GreaterThanAttachAnd("Mileage", Me.intMinMileage.Value, strDelim)
  32. strSQLContent = strSQLContent & lib.LessThanAttachAnd("Mileage", Me.intMaxMileage.Value, strDelim)
  33. strSQLContent = strSQLContent & lib.EqualsAttachAnd("MileageType", Me.cmbMileageType.Value, strDelim)
  34. strSQLContent = strSQLContent & lib.LikeAttachAnd("FailureMode/Complaint-Detailed", Me.memoFailureModeComplaintDetailed.Value, strDelim)
  35. strSQLContent = strSQLContent & lib.LikeAttachAnd("FailureMode/Complaint-Simple", Me.txtFailureModeComplaintSimple.Value, strDelim)
  36. strSQLContent = strSQLContent & lib.LikeAttachAnd("CTSAnalysis", Me.memoCTSAnalysis.Value, strDelim)
  37. strSQLContent = strSQLContent & lib.LikeAttachAnd("CTSFindings", Me.memoCTSFindings.Value, strDelim)
  38. strSQLContent = strSQLContent & lib.LikeAttachAnd("Comments", Me.memoComments.Value, strDelim)
  39. strSQLContent = strSQLContent & lib.EqualsAttachAnd("BOMNumber", Me.txtBOMNumber.Value, strDelim)
  40. strSQLContent = strSQLContent & lib.EqualsAttachAnd("CustomerLocation", Me.cmbCustomerLocation.Value, strDelim)
  41. strSQLContent = strSQLContent & lib.LikeAttachAnd("Customer", Me.txtCustomer.Value, strDelim)
  42. strSQLContent = strSQLContent & lib.EqualsAttachAnd("CorrectiveActionNumber", Me.txtCorrectiveActionNumber.Value, strDelim)
  43. strSQLContent = strSQLContent & lib.LikeAttachAnd("MiscIDNumber", "* " & Me.memoMiscIDNumber.Value, strDelim)
  44. strSQLContent = strSQLContent & lib.LikeAttachAnd("Misc", "* " & Me.memoMisc.Value, strDelim)
  45.  
  46. 'Clean up the concatenation of all of the fields into a SQL statement
  47. strSQLContent = Replace(strSQLContent, strDelim, "", 0, 1)
  48.  
  49. 'Set results to SELECT statement that can be of use
  50. BuildSQLStr = "SELECT * FROM [Return Log] WHERE " & strSQLContent & " ORDER BY [Return Log].[" & Me.cmbGroupBy.Value & "]"
  51.  
  52. End Function
It starts on line 8 of the above code. I have concluded that it's one of two things: (1) this type of function by nature doesn't like null values, or (2) the function's internal mechanism to handle null values is buggy. The first conclusion would lead me to believe there are some permissions issues that I've overlooked.

As one of the experts on this site said, "An expert is someone who's willing to learn despite their ignorance" ... or something to that effect.

I realize the conversation has veered away from the original post almost completely, but I really do appreciate the help. Should I just start a new thread to solve these debugging issues or is everyone okay with it remaining here?

EDIT: I forgot to mention, I used both of your suggestions and it seemed to get me past the aforementioned vague error message, but threw me into another one. Microsoft is awesome... (-_-)
Jun 29 '10 #8
NeoPa
32,556 Expert Mod 16PB
I think we can leave it here for the time-being at least.

Have you looked at Debugging in VBA?

Is your function declared as As String? If so, then string variables cannot contain Null values. If you need to handle Null values then you should use As Variant instead.
Jun 29 '10 #9
thelonelyghost
109 100+
@NeoPa
Actually I was just looking into that. I decided to finally look at your filter form you uploaded as a reference HERE and tried checking for null before using the function (rather than inside the function like I've been doing) and it seems to get past it the issue I've been running into. I'll try your variant suggestion since I do want to try to keep this as much a part of a module as possible. I also thought that setting the control as a string converts the null value into a zero-length string automatically. If not, (out of curiosity) is there any way to convert it? or would it be more trouble than it's worth?

EDIT: setting the type as variant instead of string worked like a charm. Now I don't need all of those repetitive if-statements cluttering up my BuildSQLStr function! The debugger is yelling at me for the Replace() function I called by saying it doesn't exist. Back to the documentation!
Jun 29 '10 #10
thelonelyghost
109 100+
EDIT: Nevermind on this post, I'll just post a new thread if I can't find one that's similar. NEW THREAD
Jun 29 '10 #11
NeoPa
32,556 Expert Mod 16PB
Replace() should be found in the VBA library.
Jun 29 '10 #12
thelonelyghost
109 100+
@NeoPa
Ah but I *was* using the VBA function Replace(), and it was in syntax! It ran into an error saying Access didn't know what function that was. After some research I found out that it's a defeciency with some versions of Access 2000. No worries though, I found a workaround that works.

If you want to see the research for yourself, google "Replace() Access 2000"
Jun 29 '10 #13
NeoPa
32,556 Expert Mod 16PB
Were you using it in your SQL by any chance? If so, then that is technically not VBA code. The VBA code creates the SQL string, which is then passed to the SQL interpreter for execution. This is a confusing area for many people, particularly because some effort has gone into making available many (most) of the functions that are available in the VBA library. I know of no way that you can add library references to make them available to the SQL interpreter I'm afraid.

This level of redirection is also the cause of many confusions and difficulties when debugging such VBA -> String -> SQL work.
Jun 30 '10 #14
thelonelyghost
109 100+
@NeoPa
No it wasn't within the SQL code. My code currently is:
Expand|Select|Wrap|Line Numbers
  1. 'Outputs string of SQL statement
  2. Public Function BuildSQLStr()
  3.  
  4. Dim strSQLContent As String
  5.   strSQLContent = ""
  6.  
  7. 'detecting whether to use AND or OR
  8. 'based on button 'ANY' or 'ALL'
  9. Dim strDelim As String
  10.   If Me.btn2SearchSettingsAnyAll.Value = 1 Then
  11.     strDelim = " OR "
  12.   Else: If Me.btn2SearchSettingsAnyAll.Value = 2 _
  13.     Then strDelim = " AND "
  14.   End If
  15.  
  16. 'transforming form inputs into SQL code with subfunctions
  17. ' PLACEHOLDER: various Equals-, Like-, GreaterThan-, and 
  18. ' LessThanAttachAnd functions for each field on my form,
  19. ' concatenating into a string, strSQLContent
  20.  
  21. 'Clean up the concatenation of all of the fields into a SQL statement fragment
  22. 'begins with 'WHERE'
  23. strSQLContent = lib.SQLFixLeadDelim(strSQLContent, strDelim, "WHERE ")
  24.  
  25. 'Outputs results to SELECT statement that can be of use
  26. BuildSQLStr = "SELECT * FROM [tblReturnLog] " & strSQLContent & " ORDER BY [tblReturnLog].[" & Me.cmbGroupBy.Value & "]"
  27.  
  28. End Function
If you look on line 23 of the code, that's where the old replace function was located. I replaced it with my own that is tailored to my situation. The location of this is very much in the midst of the other VBA code. The new replace function is a modification of a workaround I found using google, authored by Alden Streeter.

It has three inputs where you have the string to replace within (strInString), the string to search for (strFindStr), and the string to replace it with (strReplaceStr). The (unmodified) code essentially searches for the first instance of strFindStr, writes the output (FindAndReplace) based on strInString until the first instance of strFindStr, appends strReplaceStr to FindAndReplace, appends the rest of strInString after strFindStr to FindAndReplace, then finally sets strInString to FindAndReplace. It loops this sequence until it doesn't find an instance of strFindStr. My modification merely took out that loop.

Sorry, I would have posted the code, but it's apparently copywritten and part of the agreement is not to distribute any modified version of the code unless it's part of an application.
Jun 30 '10 #15
NeoPa
32,556 Expert Mod 16PB
Sorry. I wasn't trying to imply you were likely to have been confused by that issue. I was simply trying to process the information I had, and come up with the most reasonable interpretation. In this case I was in error. It doesn't surprise me greatly as I've caught a number of your posts already and, though you're new to the site, you're clearly no newbie.

My Google search, duplicating that which you posted, also led me to believe that this issue only occurred within DAO (essentially Jet SQL). Otherwise I was unable to find an explanation. It always worked for me. Sorry.

BTW. Good for you not posting anything you feel is copyrighted.
Jun 30 '10 #16
thelonelyghost
109 100+
@NeoPa
I understand now. Thinking out loud, if you will. I also see that you understood more of the research than I did. I took it to mean that some versions of Access 2000 had issues with the Replace() function. In the interest of having a wider base of users (all users with Access 2000 and above), I defined a function that wasn't buggy.

As for my newbie status on these forums, I am a newbie with Access; I am not a newbie to programming nor am I a newbie to forum etiquette. Thanks to these forums complimenting my learning style, I'm quickly learning more about Access, VBA, and SQL. Working with this software would be a LOT harder without some sort of usergroup like this, so it's very much appreciated!

Although I personally have no qualms with passing along copyrighted materials and am a hardcore fan of the open source ideals, I don't want to bring any rain of legal action down on your host or the site itself. That above all else is why I didn't post the code.
Jun 30 '10 #17
NeoPa
32,556 Expert Mod 16PB
It sounds like you'll fit in here very well thank you :) All good sentiments.
Jun 30 '10 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Donald Thompson | last post by:
How do I prevent the output of a user defined function from going to the browser? In my script I do something like: $MyVar = MyFunction($var1,$var); which works, except that the output from...
1
by: Richard Hollenbeck | last post by:
I noticed I can't push a value into a text box by saying something like, "txtThisTextBox = intSomeVariable * 0.5" because I get an run-time error saying I can't assign a value to this object....
8
by: Michel Rouzic | last post by:
I'm sure that it's a dumb problem where i surely did something dumb, but, I got a problem, I got a variable that we'll call var1. in the main() function, this variable var1 (of type unsigned) of a...
1
by: bruce | last post by:
hi... i have the following test python script.... i'm trying to figure out a couple of things... 1st.. how can i write the output of the "label" to an array, and then how i can select a given...
5
by: Rares Vernica | last post by:
Hi, I am trying to write a template that will take any type of vector and output it in a certain way. Here is my code: #include <ostream> #include <vector> #include <iterator> using...
2
by: Oli Thissen | last post by:
Hello everybody! I'm having a little problem with the following: I wrote a class to collect data (Let's call it DataCollector). Now I want the data to be interpreted in various ways. My idea was...
4
by: sebastien.willemijns | last post by:
hello, i use a small PHP script to grab DNS infos at http://80.247.230.136/bug/php/ (php and txt available) the trouble is the last line of every output given by "system" function is always...
2
by: Deathtotock | last post by:
I am inputing from a file and I am outputting to a file. well when i run the program it just puts the headers up there and no names or grades. This is the input. Balto 85 83 77 91 76...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.