By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,072 Members | 1,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,072 IT Pros & Developers. It's quick & easy.

Force .DefaultValue to be a string

P: 74
I have a sub to determine if overtime has come into play yet on a certain job. If (preapproved) overtime has started then the next (subform) record can automatically be populated from a query checking the overtime allowance. Here is the code.
Expand|Select|Wrap|Line Numbers
  1. Private Sub OT_Test()
  2. If Forms!frmJobHourCostEmpName.TotalHours = 8 Then
  3. Me.OTJob.DefaultValue = True
  4. Me.BilledHours.DefaultValue = DLookup("OTApproval", "qryOTAllowance")
  5. Me.JobNumber.DefaultValue = DLookup(CStr("JobNo"), "qryOTAllowance")
  6. Me.BilledPercentage.DefaultValue = BilledHours / (8 + Nz(Forms!frmJobHourCostEmpName.OTHours, 0))
  7. End If
  8. End Sub
The issue is that Job Numbers are in a format of:

Example: "12345-1" or "12345-2" or "65487-1"...ok you get it.

you may have noticed the DLookup(CStr("JobNo"),"qryOTAllowance")

The actual field in the query is JobNo: Cstr(Jobnumber)

I even tried
Expand|Select|Wrap|Line Numbers
  1. Dim JobNumberTemp as string
  2. JobNumberTemp = DLookup(CStr("JobNo"), "qryOTAllowance")
  3. Me.JobNumber.DefaultValue  = JobNumberTemp
In hoping that the "JobNumber.DefaultValue" would remain a string ("12345-2") instead of a math problem (12345-2 = 12343) because all that i can get to display is "12343" I noticed that if i dont use defaultvalue, i get the actual string, but obviously on the wrong record (the current record, instead of the next record)

Does anybody have any ideas of how to accomplish this? Its driving me nuts. As always..... Thanks for any help.
Jun 3 '09 #1
Share this Question
Share on Google+
25 Replies

Expert 2.5K+
P: 2,653
Does it help to add text delimiters to the string assigned to DefaultValue property?

Jun 3 '09 #2

Expert 100+
P: 1,287
Expand|Select|Wrap|Line Numbers
  1. Me.JobNumber.DefaultValue  = """" & JobNumberTemp & """"
Jun 3 '09 #3

P: 74
Thanks ChipR and FishVal i never tried it, because originally i thinking about 1 set of quotes which would have been a literal sting but i never thought about """" & so thanks. It worked great.
Jun 3 '09 #4

Expert Mod 15k+
P: 31,709
Would it not work if the CStr() call were outside of the DLookup(), rather than within it?
Jun 4 '09 #5

Expert 2.5K+
P: 3,532
I'm really confused here! If the values in this field are in a format of:

"12345-1" or "12345-2" or "65487-1"

then the field is defined as Text. You cannot enter these values in a field that is defined as a Number datatype.Why do you need to use CStr()? Using Chip's suggestion alone should do it.

Linq ;0)>
Jun 4 '09 #6

Expert Mod 15k+
P: 31,709
Quite right. I skimmed over the penultimate paragraph in the OP too quickly it seems.

In that case Chip is indeed on the ball (again).

I would do it very slightly differently for reasons explained in Quotes (') and Double-Quotes (") - Where and When to use them.
Expand|Select|Wrap|Line Numbers
  1. Me.JobNumber.DefaultValue = "'" & Me.JobNumberTemp & "'"
Jun 4 '09 #7

P: 74
I did use Chips solution and it did work. Originally the query was doing the math problem instead of correctly reporting it as text. Then (only when i set .defaultvalue = tempjobnumber) it was still doing the math. I havent had a chance to use NeoPa's advice because once it worked.... I stopped, lol.

OFF TOPIC Can you hijack your own thread? :)
I never usually use
Expand|Select|Wrap|Line Numbers
  1. """"
i almost always use
Expand|Select|Wrap|Line Numbers
  1. "'"
although i know it will get me in trouble one day ( actually it already has with an emplyees last name - O'Bryant). I REALLY struggle when it comes to text delimeters, i have read the tutorial on here about it, but it doesnt really help me.....(well it does with individual fields, but when i try to do
Expand|Select|Wrap|Line Numbers
  1. docmd.openreport "rptWhatEver",acnormal,,"stuff='" & text & "'" and anything else
with multiple criteria, the confusion really begins.

and sometimes i even have to change that to
Expand|Select|Wrap|Line Numbers
  1. docmd.openreport "rptWhatEver",acnormal,,"stuff=" & "'" & text & "'" and anything else
And i havenet really quite figured out why... i tend to use the first one most of the time, now that i think about it, iam not really sure which one ends up giving me the problems.
Jun 4 '09 #8

Expert Mod 15k+
P: 31,709
Such problems can occur either way in fact.
EG. {O'Brien}
{4" by 2" plank}.
" is a sign for Inches if you weren't aware of that.

Doubling-up quotes in the data is generally a way to get past this.

The correct (standard) character for quotes in SQL strings is ('). Access uses (") by default instead (M$ were stupid enough to think that this would simplify matters rather than the fairly predictable result of confusing people greatly). In Access either will work though (at least until ANSI-92 becomes the default, which is unlikely to be far away).

To program such problems properly, it is advisable to pass any such references through a filter function so that any such quotes get doubled for you automatically if they occur.

I hope this helps and clarifies things for you a bit.
Jun 4 '09 #9

Expert 100+
P: 1,287
The best way to figure this out is to use an intermediate variable so that you can check the value of the string. This way, you will quickly see where you made a mistake.

Expand|Select|Wrap|Line Numbers
  1. Dim strCriteria as String
  2. strCriteria = "[field1] = """ & variable & """"
  3. MsgBox strCriteria    'or Debug
  4. DLookup("field2", "table1", strCriteria)
Jun 4 '09 #10

Expert Mod 15k+
P: 31,709
That's certainly a sensible approach for the development stage. Less necessary for when the code is perfected.

Personally I use the debugger (Debugging in VBA) to see the values of variables, but anything that works for you is good. The important thing is to have access to the information when developing. Either method produces this.

I cannot stress too much how much help debugging techniques can be while developing your projects. A very good point to raise :)
Jun 4 '09 #11

P: 3
I don't know if this the amateur way of doing it or not (or maybe just the lazy way), but I like to deal with quotes like this:

Expand|Select|Wrap|Line Numbers
  1. Dim strCriteria As String
  2. Dim ChrQuote As String
  3. ChrQuote = Chr(34)  ' Chr(34) is the character code for a quote sign
  4. strCriteria = "[field1] = " & ChrQuote & strVariable & ChrQuote
  5. DLookup("field2", "table1", strCriteria)
That way, you don't have to worry about the double-quote and single-quote (Chr(39)) rules, you can just string variables together like you want.

It also works with message box messages when you want to include a CR/LF (Chr(13) and Chr(10) respectively).
Jun 4 '09 #12

Expert 100+
P: 1,287
Yes, that does work. I thought about it, and would have written that in my original post, but I can never remember Chr(34). I often use vbCrLf, maybe they should make a vbQuote.
Jun 4 '09 #13

P: 74
Anybdoy have any issues with Chr(34)? Sounds easy. Is there a difference between vbCrLf, and vbCr?

*Edit* I was working on my project came back across the name O'Bryant, and was wondering.... with the Chr(34)... wouldnt that fall into the same single quote trap?
Jun 4 '09 #14

Expert Mod 15k+
P: 31,709
There seems to be a misunderstanding here.

This actually gains you nothing, as the same rules apply whichever way you produce the quotes.

All you have done is made the code less easy to read and understand.

As far as CRs (etc) go :
Expand|Select|Wrap|Line Numbers
  1. vbCrLf --> Carriage Return + Line Feed --> Chr(13) & Chr(10)
  2. vbCr   --> Carriage Return             --> Chr(13)
  3. vbLf   --> Line Feed                   --> Chr(10)
Again, I see no benefit in obscuring what you are doing by using a Chr() call.
Jun 4 '09 #15

Expert Mod 15k+
P: 31,709
As They in this case would be the same people that caused the confusion in the first place I doubt they would even use the correct one.
Jun 4 '09 #16

Expert 100+
P: 1,287
That was my first reaction, but I tested out composing strings with Chr(39), and it allows you to assign strings with quotes in them properly.
Jun 4 '09 #17

Expert Mod 15k+
P: 31,709
Historically, Carriage Return returned the carriage of a teletype or typewriter type device to the left hand side. A Line Feed fed a line of text.

When the devices were still mechanical, it was necessary to send them in that order so that the Carriage Return started earlier. It took longer and the Line Feed could be executed while the carriage was still in transit.

More recently, it is standard on a PC using any form of windows that the text files separate their lines with both characters. However, Unix and other Nix OSes have a standard where the text lines are separated with the Carriage Return character only. Sometimes you may deal with vbCR, and others with vbCrLf.

Technically, the enter key is actually returns the vbCr character, but this is often translated on the fly in Windows to both characters (vbCrLf).
Jun 4 '09 #18

Expert Mod 15k+
P: 31,709
Perhaps you could clarify what you were referring to here. I'm afraid I may have obscured the conversation by posting between.

I can't actually think of a scenario where that usage helps anything.
Jun 4 '09 #19

Expert 100+
P: 1,287
Never mind on that last post! That's what I get for not testing well. Obviously Chr(34) and """" both let you compose strings with quotes in them and assign them to variables in vba. It's when you try to use those strings with SQL functions that they both fail.
Jun 4 '09 #20

Expert Mod 15k+
P: 31,709
Indeed Chip.

As this is the main crux of the problem here, I perhaps ought to flesh out somewhat what I was saying earlier. The function could be :
Expand|Select|Wrap|Line Numbers
  1. Public Function DoQuotes(strField As String, _
  2.                          Optional ByVal strQuote As String = "'") As String
  3.     DoQuotes = Replace(strField, strQuote, strQuote & strQuote)
  4. End Function
This allows for usage with either quote character. You can even choose to change the default to what you are most comfortable with yourself.

Now, assuming you have a TextBox control on your form that you wish to use as a filter for some SQL, you might use code similar to this to make use of it :
Expand|Select|Wrap|Line Numbers
  1. ...
  2. strSQL = "SELECT ... " & _
  3.          "FROM ... " & _
  4.          "WHERE [YourField] Like '*" & DoQuotes(Me.TextBoxControl) & "*'"
Jun 4 '09 #21

Expert Mod 15k+
P: 31,709
A new thread (SQL Injection Attack Discussion) has been created to handle the discussion of SQL Injection Attacks.

It is related to the original but not too directly.
Jun 4 '09 #22

Expert Mod 2.5K+
P: 2,545
Could I just mention with regard to the very first post in this thread - about the default value issue - ALL the lines in the code snippet which refer to the DefaultValue property are incorrectly formed. Regardless of the type of the value you wish to set, the defaultvalue property is a string. This does indeed mean ensuring that quotes are used to enclose such values - which is what Chip's solution did. ALL the ones shown in the code snippet should be set inside quotes accordingly - so

xxx.defaultvalue = true

should really be

xxx.defaultvalue = "'" & true & "'"

although the VBA interpreter will do its best to convert values on the fly to match the string type of the defaultvalue property.

What happened in this particular case (the 12345-1 etc issue) is that without the quotes the VBA interpreter treated the default value setting as an arithmetic expression involving two constants, which it replaced with the result of the expression. If SomeVar represents a variable with a text value of "12345-1", then writing

fieldname.defaultvalue = SomeVar

will result in the interpreter substituting the value of SomeVar as an expression

12345 - 1 (result 12344)

This is quite different to setting it correctly inside quotes:

fieldname.defaultvalue = "'" & SomeVar & "'"

resulting in the default value showing correctly as 12345-1 in the control.

An extract from the MS help entry gives an example, though unfortunately not one relating to expressions:

In Visual Basic, use a string expression to set the value of this property. For example, the following code sets the DefaultValue property for a text box control named PaymentMethod to "Cash":

Forms!frmInvoice!PaymentMethod.DefaultValue = """Cash"""
Jun 4 '09 #23

P: 3
As far as CRs (etc) go, I totally agree with you. I was using those character codes back in Access 2.0 where I don't think (but I could be wrong) there was a vbCr, etc.

As far as quotes go, I guess it is just what I am used to but these two lines produce the same results:
Expand|Select|Wrap|Line Numbers
  1. strCriteria = "[field1] = """ & variable & """"
  2. strCriteria = "[field1] = " & ChrQuote & variable & ChrQuote
To me, the second one is actually easier to code and to understand because you don't have to worry about when to use triple or quadruple quotes. Of course, the rules about quotes in the result string are the same, I just meant you don't have to worry about when to use """ or """" or whatever. Maybe its just me, sorry if I confused things.
Jun 5 '09 #24

Expert 2.5K+
P: 2,653
It could be done either way which seems suitable, beautiful or even the only right one for a particular coder .

Additionally, Access.Application class has BuildCriteria() method which build criteria expression with "appropriate" (quotes because it uses double quotes to introduce string constant into resulting expression <uuuuf>) delimiters.

However, all those methods have nothing to do with delimiter symbols already existing in criteria. Approach suggested by NeoPa to preprocess string with a simple VBA code is, IMHO, very suitable solution.
Jun 5 '09 #25

Expert Mod 15k+
P: 31,709

Let me start by saying that, not only do you have every right to post your thoughts, however right or wrong they are, but we always appreciate any input, such as yours, which is intended to be helpful or share your experience. This doesn't mean that we won't necessarily post comments in response. Just that any such responses are part of a discussion where any or all can make their own particular views known. That way we all pick up ideas, or ways of looking at things, that we may not have considered before.

Those of us here who answer questions are always picking up new things from others. Speaking for myself I know this happens regularly.

Further comment on your latest post :

You referred in your earlier post to Chr(34) rather than ChrQuote. Either way, it does make it clearer you're dealing with quotes, but when you're reading the code you're not generally looking just for that. You want a good understanding of the whole picture. If the focus is so much on the quotes it makes reading the string more difficult (to my mind at least).

As for doubling-up the quotes when working on SQL within VBA - This is generally unnecessary. As Access VBA uses the Double-Quote and the SQL standard is to use Single-Quotes there should not be any issue. Using these correctly also makes working in such code so much easier to read and understand. If people insist on using the Double-Quotes within SQL this can still work, it just makes things more complicated.

Just to finish by repeating the most important point - You have every right to express your opinion, and we value all contributions, whether they fit in with our own views or not.

Welcome to Bytes!
Jun 5 '09 #26

Post your reply

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