473,396 Members | 1,891 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,396 software developers and data experts.

Force .DefaultValue to be a string

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
  4.  
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
25 5580
FishVal
2,653 Expert 2GB
Does it help to add text delimiters to the string assigned to DefaultValue property?

Regards,
Fish.
Jun 3 '09 #2
ChipR
1,287 Expert 1GB
Try
Expand|Select|Wrap|Line Numbers
  1. Me.JobNumber.DefaultValue  = """" & JobNumberTemp & """"
Jun 3 '09 #3
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
NeoPa
32,556 Expert Mod 16PB
Would it not work if the CStr() call were outside of the DLookup(), rather than within it?
Jun 4 '09 #5
missinglinq
3,532 Expert 2GB
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
NeoPa
32,556 Expert Mod 16PB
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
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
  2.  
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
NeoPa
32,556 Expert Mod 16PB
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
ChipR
1,287 Expert 1GB
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
NeoPa
32,556 Expert Mod 16PB
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
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
ChipR
1,287 Expert 1GB
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
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
NeoPa
32,556 Expert Mod 16PB
@HankWalters
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
NeoPa
32,556 Expert Mod 16PB
@ChipR
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
ChipR
1,287 Expert 1GB
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
NeoPa
32,556 Expert Mod 16PB
@kstevens
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
NeoPa
32,556 Expert Mod 16PB
@ChipR
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
ChipR
1,287 Expert 1GB
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
NeoPa
32,556 Expert Mod 16PB
@NeoPa
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
NeoPa
32,556 Expert Mod 16PB
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
Stewart Ross
2,545 Expert Mod 2GB
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"""
-Stewart
Jun 4 '09 #23
@NeoPa
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
FishVal
2,653 Expert 2GB
@HankWalters
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
NeoPa
32,556 Expert Mod 16PB
@HankWalters
Hank,

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

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

Similar topics

0
by: Craig Schneider | last post by:
// Is there any way to override the XML Serialization of the following SimpleClass // to turn off the DefaultValue of a boolean? Sure, I can override the DefaultValue from // true to false, but...
2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
1
by: Mark | last post by:
How do I write the following code to set the default value? Me!xNum.DefaultValue = Me!XCurrency.DefaultValue = Me!XDate.DefaultValue = Me!XString.DefaultValue = Can I just put all the lines...
3
by: Bram Vandendriesschen | last post by:
Hi, anyone an idea how to get DefaultValue's working for String properties in managed C++. I wrote a component in managed C++ which I plan to consume in C#. Now the IDE keeps inserting...
1
by: RR | last post by:
Hi, I want to set the defaultvalue of a property in a class. I know this is possible by the DefaultValue Attribute like this: public class Test { public Test(){}
5
by: schneider | last post by:
Hello, Have an issue with a property using the DefaultValue(True) attribute. Imports System.ComponentModel Public Class Class1 Private m_testValue As Boolean
6
by: Meelis Lilbok | last post by:
Hi Why does property default value not work? When i start my application and dont set value from properties window the value is always "nothing". Whats the point off DefualtValue then? ...
3
by: MLH | last post by:
On frmVehicleEntryForm... I wanted to change the DefaultValue property of one of my textbox controls on a form from within code. I wanted the setting to be no setting - as if nothing had ever...
10
by: tony | last post by:
Hello!! I have some demo programs written in C# and they have this construction "" see below. I haven't seen this before so what does it mean ? public bool ShowDropDownButtons { get {...
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.