Visual Basic 6 String Functions

Level:
Level2

Originally Written By TheVBProgramer. Cleaned up and reformatted for this site.

$ReqTestHarness$

VB has numerous built-in string functions for processing strings. Most VB string-handling functions return a string, although some return a number (such as the Len function, which returns the length of a string and functions like Instr and InstrRev, which return a character position within the string). The functions that return strings can be coded with or without the dollar sign ($) at the end, although it is more efficient to use the version with the dollar sign.

The first time I started trying to understand the VB6 string functions I was somewhat confused. This tutorial will walk you through all the different ways you can us VB to handle strings. If you are still confused feel free to post a comment and hopefully we can help get you cleared up. Also there are many other string related tutorials on this site so feel free to browse around.

Function:

Len

Description:

Returns a Long containing the length of the specified string

Syntax:

Len(string)

Where string is the string whose length (number of characters) is to be returned.

Example:

lngLen = Len("Visual Basic")    ' lngLen = 12

 

Function:

Mid$ (or Mid)

Description:

Returns a substring containing a specified number of characters from a string.

Syntax:

Mid$(string, start[, length])

The Mid$ function syntax has these parts:

string Required. String expression from which characters are returned.

start Required; Long. Character position in string at which the part to be taken begins. If start is greater than the number of characters in string, Mid returns a zero-length string ("").

length Optional; Long. Number of characters to return. If omitted or if there are fewer than length characters in the text (including the character at start), all characters from the start position to the end of the string are returned.

 

Example:

 

strSubstr = Mid$("Visual Basic", 3, 4)          ' strSubstr = "sual"

Note: Mid$ can also be used on the left side of an assignment statement, where you can replace a substring within a string.

strTest = "Visual Basic"
Mid$(strTest, 3, 4) = "xxxx"

'strTest now contains "Vixxxx Basic"

In VB6, the Replace$ function was introduced, which can also be used to replace characters within a string.

 

Function:

Left$ (or Left)

Description:

Returns a substring containing a specified number of characters from the beginning (left side) of a string.

Syntax:

Left$(string, length)

The Left$ function syntax has these parts:

string Required. String expression from which the leftmost characters are returned.

length Required; Long. Numeric expression indicating how many characters to return. If 0, a zero-length string ("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.

 

Example:

strSubstr = Left$("Visual Basic", 3)      ' strSubstr = "Vis"

' Note that the same thing could be accomplished with Mid$:
strSubstr = Mid$("Visual Basic", 1, 3)

 

Function:

Right$ (or Right)

Description:

Returns a substring containing a specified number of characters from the end (right side) of a string.

Syntax:

Right$(string, length)

The Right$ function syntax has these parts:

string Required. String expression from which the rightmost characters are returned.

length Required; Long. Numeric expression indicating how many characters to return. If 0, a zero-length string ("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.

Example:

strSubstr = Right$("Visual Basic", 3)     ' strSubstr = "sic"

' Note that the same thing could be accomplished with Mid$:
strSubstr = Mid$("Visual Basic", 10, 3)

 

Function:

UCase$ (or UCase)

Description:

Converts all lowercase letters in a string to uppercase. Any existing uppercase letters and non-alpha characters remain unchanged.

Syntax:

UCase$(string)

Example:

strNew = UCase$("Visual Basic")           ' strNew = "VISUAL BASIC"

 

Function:

LCase$ (or LCase)

Description:

Converts all uppercase letters in a string to lowercase. Any existing lowercase letters and non-alpha characters remain unchanged.

Syntax:

LCase$(string)

Example:

strNew = LCase$("Visual Basic")           ' strNew = "visual basic"

 

Function:

Instr

Description:

Returns a Long specifying the position of one string within another. The search starts either at the first character position or at the position specified by the start argument, and proceeds forward toward the end of the string (stopping when either string2 is found or when the end of the string1 is reached).

Syntax:

InStr([start,] string1, string2 [, compare])

The InStr function syntax has these parts:

start Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. The start argument is required if compare is specified.

string1 Required. String expression being searched.

string2 Required. String expression sought.

compare Optional; numeric. A value of 0 (the default) specifies a binary (case-sensitive) search. A value of 1 specifies a textual (case-insensitive) search.

Examples:

lngPos = Instr("Visual Basic", "a")
' lngPos = 5

lngPos = Instr(6, "Visual Basic", "a")
' lngPos = 9       (starting at position 6)

lngPos = Instr("Visual Basic", "A")
' lngPos = 0       (case-sensitive search)

lngPos = Instr(1, "Visual Basic", "A", 1)
' lngPos = 5       (case-insensitive search)

 

Function:

InstrRev

Description:

Returns a Long specifying the position of one string within another. The search starts either at the last character position or at the position specified by the start argument, and proceeds backward toward the beginning of the string (stopping when either string2 is found or when the beginning of the string1 is reached).

Introduced in VB 6.

Syntax:

InStrRev(string1, string2[, start, [, compare]])

The InStr function syntax has these parts:

string1 Required. String expression being searched.

string2 Required. String expression sought.

start Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the last character position.

compare Optional; numeric. A value of 0 (the default) specifies a binary (case-sensitive) search. A value of 1 specifies a textual (case-insensitive) search.

Examples:

lngPos = InstrRev("Visual Basic", "a")
' lngPos = 9

lngPos = InstrRev("Visual Basic", "a", 6)
' lngPos = 5 (starting         at position 6)

lngPos =       InstrRev("Visual Basic", "A")
' lngPos = 0         (case-sensitive search)

lngPos =       InstrRev("Visual Basic", "A", , 1)
' lngPos = 9         (case-insensitive search)
' Note         that this last example leaves a placeholder for the start argument

Notes on Instr and InstrRev:

·         Something to watch out for is that while Instr and InstrRev both accomplish the same thing (except that InstrRev processes a string from last character to first, while Instr processes a string from first character to last), the arguments to these functions are specified in a different order. The Instr arguments are (start, string1, string2, compare) whereas the InstrRev arguments are (string1, string2, start, compare).

·         The Instr function has been around since the earlier days of BASIC, whereas InstrRev was not introduced until VB 6.

·         Built-in "vb" constants can be used for the compare argument:

vbBinaryCompare for 0 (case-sensitive search)
vbTextCompare for 1 (case-insensitive search)

 

Function:

String$ (or String)

Description:

Returns a string containing a repeating character string of the length specified.

Syntax:

String$(number, character)

The String$ function syntax has these parts:

number Required; Long. Length of the returned string.

character Required; Variant. This argument can either be a number from 0 to 255 (representing the ASCII character code* of the character to be repeated) or a string expression whose first character is used to build the return string.

Examples:

strTest = String$(5, "a")
' strTest = "aaaaa"

strTest = String$(5, 97)
' strTest = "aaaaa" (97 is the ASCII code for "a")

* A list of the ASCII character codes is presented at the end of this topic.

Function:

Space$ (or Space)

Description:

Returns a string containing the specified number of blank spaces.

Syntax:

Space$(number)

Where number is the number of blank spaces desired.

 

Examples:

strTest = Space$(5)     ' strTest = "     "

 

Function:

Replace$ (or Replace)

Description:

Returns a string in which a specified substring has been replaced with another substring a specified number of times.

Introduced in VB 6.

Syntax:

Replace$(expression, find, replacewith[, start[, count[, compare]]])

The Replace$ function syntax has these parts:

expression Required. String expression containing substring to replace.

find Required. Substring being searched for.

replacewith Required. Replacement substring.

start Optional. Position within expression where substring search is to begin. If omitted, 1 is assumed.

count Optional. Number of substring substitutions to perform. If omitted, the default value is –1, which means make all possible substitutions.

compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. (0 = case sensitive, 1 = case-insensitive)

Built-in "vb" constants can be used for the compare argument:

vbBinaryCompare for 0 (case-sensitive search)
vbTextCompare for 1 (case-insensitive search)

Examples:

strNewDate = Replace$("08/31/2001", "/", "-")
' strNewDate = "08-31-2001"

 

Function:

StrReverse$ (or StrReverse)

Description:

Returns a string in which the character order of a specified string is reversed.

Introduced in VB 6.

Syntax:

StrReverse$(string)

Examples:

strTest = StrReverse$("Visual Basic")           ' strTest = "cisaBlausiV"

 

 

Function:

LTrim$ (or LTrim)

Description:

Removes leading blank spaces from a string.

Syntax:

LTrim$(string)

Examples:

strTest =         LTrim$("  Visual Basic  ")
' strTest =         "Visual Basic  "

 

Function:

RTrim$ (or RTrim)

Description:

Removes trailing blank spaces from a string.

Syntax:

RTrim$(string)

Examples:

strTest = RTrim$("Visual Basic")      ' strTest = "Visual Basic"

 

Function:

Trim$ (or Trim)

Description:

Removes both leading and trailing blank spaces from a string.

Syntax:

Trim$(string)

Examples:

strTest = Trim$("  Visual Basic  ")       ' strTest = "Visual Basic"
' Note: Trim$(x) accomplishes the same thing as LTrim$(RTrim$(x))

 

Function:

Asc

Description:

Returns an Integer representing the ASCII character code corresponding to the first letter in a string.

Syntax:

Asc(string)

Examples:

intCode = Asc("*")      ' intCode = 42
intCode = Asc("ABC")    ' intCode = 65

 

Function:

Chr$ (or Chr)

Description:

Returns a string containing the character associated with the specified character code.

Syntax:

Chr$(charcode)

Where charcode is a number from 0 to 255 that identifies the character.

Examples:

strChar = Chr$(65)     ' strChar = "A"

 

ASCII Character Codes (0 through 127)

0

N/A

32

[space]

64

@

96

`

1

N/A

33

!

65

A

97

a

2

N/A

34

"

66

B

98

b

3

N/A

35

#

67

C

99

c

4

N/A

36

$

68

D

100

d

5

N/A

37

%

69

E

101

e

6

N/A

38

&

70

F

102

f

7

N/A

39

'

71

G

103

g

8

(backspace)

40

(

72

H

104

h

9

(tab)

41

)

73

I

105

i

10

(line feed)

42

*

74

J

106

j

11

N/A

43

+

75

K

107

k

12

N/A

44

,

76

L

108

l

13

(carriage return)

45

-

77

M

109

m

14

N/A

46

.

78

N

110

n

15

N/A

47

/

79

O

111

o

16

N/A

48

0

80

P

112

p

17

N/A

49

1

81

Q

113

q

18

N/A

50

2

82

R

114

r

19

N/A

51

3

83

S

115

s

20

N/A

52

4

84

T

116

t

21

N/A

53

5

85

U

117

u

22

N/A

54

6

86

V

118

v

23

N/A

55

7

87

W

119

w

24

N/A

56

8

88

X

120

x

25

N/A

57

9

89

Y

121

y

26

N/A

58

:

90

Z

122

z

27

N/A

59

;

91

[

123

{

28

N/A

60

<

92

\

124

|

29

N/A

61

=

93

]

125

}

30

N/A

62

>

94

^

126

~

31

N/A

63

?

95

_

127

N/A

N/A = These characters aren't supported by Microsoft Windows.

 

ASCII Character Codes (128 through 255) 

128

N/A

160

[space]

192

À

224

à

129

N/A

161

¡

193

Á

225

á

130

N/A

162

¢

194

Â

226

â

131

N/A

163

£

195

Ã

227

ã

132

N/A

164

¤

196

Ä

228

ä

133

N/A

165

Â¥

197

Ã…

229

Ã¥

134

N/A

166

¦

198

Æ

230

æ

135

N/A

167

§

199

Ç

231

ç

136

N/A

168

¨

200

È

232

è

137

N/A

169

©

201

É

233

é

138

N/A

170

ª

202

Ê

234

ê

139

N/A

171

«

203

Ë

235

ë

140

N/A

172

¬

204

Ì

236

ì

141

N/A

173

­

205

Í

237

í

142

N/A

174

®

206

ÃŽ

238

î

143

N/A

175

¯

207

Ï

239

ï

144

N/A

176

°

208

Ð

240

ð

145

N/A

177

±

209

Ñ

241

ñ

146

N/A

178

²

210

Ã’

242

ò

147

N/A

179

³

211

Ó

243

ó

148

N/A

180

´

212

Ô

244

ô

149

N/A

181

µ

213

Õ

245

õ

150

N/A

182

¶

214

Ö

246

ö

151

N/A

183

·

215

×

247

÷

152

N/A

184

¸

216

Ø

248

ø

153

N/A

185

¹

217

Ù

249

ù

154

N/A

186

º

218

Ú

250

ú

155

N/A

187

»

219

Û

251

û

156

N/A

188

¼

220

Ü

252

ü

157

N/A

189

½

221

Ý

253

ý

158

N/A

190

¾

222

Þ

254

þ

159

N/A

191

¿

223

ß

255

ÿ

N/A = These characters aren't supported by Microsoft Windows.

The values in the table are the Windows default. However, values in the ANSI character set above 127 are determined by the code page specific to your operating system.

"Try It" Example

To demonstrate the built-in string functions, set up a "Try It" project, and place the following code in the cmdTryIt_Click event:

Private Sub cmdTryIt_Click()
    Dim strTest As String

    strTest = InputBox("Please enter a string:")

    Print "Using Len:"; Tab(25); Len(strTest)
    Print "Using Mid$:"; Tab(25); Mid$(strTest, 3, 4)
    Print "Using Left$:"; Tab(25); Left$(strTest, 3)
    Print "Using Right$:"; Tab(25); Right$(strTest, 2)
    Print "Using UCase$:"; Tab(25); UCase$(strTest)
    Print "Using LCase$:"; Tab(25); LCase$(strTest)
    Print "Using Instr:"; Tab(25); InStr(strTest, "a")
    Print "Using InstrRev:"; Tab(25); InStrRev(strTest, "a")
    Print "Using LTrim$:"; Tab(25); LTrim$(strTest)
    Print "Using RTrim$:"; Tab(25); RTrim$(strTest)
    Print "Using Trim$:"; Tab(25); Trim$(strTest)
    Print "Using String$ & Space$:"; Tab(25); String$(3, "*") _
        & Space$(2) _
        & Trim$(strTest) _
        & Space$(2) _
        & String$(3, 42)
    Print "Using Replace$:"; Tab(25); Replace$(strTest, "a", "*")
    Print "Using StrReverse$:"; Tab(25); StrReverse$(strTest)
    Print "Using Asc:"; Tab(25); Asc(strTest)
End Sub

Run the project and click the "Try It" button. When the input box comes up, enter a string of your choice.

 Some tips on what to enter:

  • To see the effects of UCase$ and LCase$, enter a mixed case string.
  • To compare Instr and InstrRev, enter a string with at least two "a"s in it.
  • To see the effects of LTrim$, RTrim$, and Trim$, enter a string with leading and/or trailing spaces.
  • To see the effect of Replace$, enter a string with at least one "a" in it.

You can also modify the code and run the project to see if you get the results you expect.

The screen shot below shows a run of the project using the code above where the string Visual Basic was input: 

Download the VB project code for the example above here.

Waterbeds great post! if you

Waterbeds
great post! if you are looking for an easy weight loss solution, may I recommend my method?

Hi, I'm unable to find the

Hi,
I'm unable to find the difference between functions with and without $,some thing like Strng() and String$(). plz give rep asap.

programming book for beginners

i will be grateful if someone can help me access a programming book. i am a novice in this field but have much interest in the subject recently. counting on you.

Programming Book

In google search for "Visual Basic Course Notes by Lou Tylee". You'll eventually find the book. This book is simply awesome.

validation

hi,i would like to know,how do you validate a piece of string:
lets say the user is required to enter a string of 10 letters which are between a-f.(eg aaaaffbced)
And the program is required to check to see if each letter that the user has typed is within the a-f range so that if the input contains an out of range letter(eg aaagwsdfee) the program can then display an error message (eg msgbox("please enter letters that are in a-f range")
PLEASE HELP ME.

string program in VB

hi...
program to find similar characters in a string and position of the characters. Ex: If the string is "Excellent" output should be there are 3 "e" and the position of the each "e"

Vb6 String Operation

Hi there, i want to remove a blank space between more than words how can i achieve that in vb6
example:
word="Visual Basic"

i want it to become
word="VisualBasic"

outstanding info available

this is a comprehensive knowledge abt string functions you have posted.

Require at least one letter and number in text box

Trying to do validations for a password and the only part I am stuck on is checking the text box for both alpha and numberic characters. There must be at least 1 letter and at least 1 number. Planning for the possibility of complex passwords, I would like to not limit to only alpha numeric and also accept special characters. But for now at least just validate that the text box is alpha numeric. Thanks in advance.

Try This

Text1.text = "AbC1"

Private Sub Command1_Click()
For x = 1 To Len(Text1.Text)
If Asc(Mid(Text1.Text, x, 1)) > 47 And Asc(Mid(Text1.Text, x, 1)) < 58 Then
iNumber = True
Exit For
End If
Next x

For x = 1 To Len(Text1.Text)
If Asc(Mid(Text1.Text, x, 1)) > 96 And Asc(Mid(Text1.Text, x, 1)) < 123 Then
iLowerCase = True
Exit For
End If
Next x

For x = 1 To Len(Text1.Text)
If Asc(Mid(Text1.Text, x, 1)) > 64 And Asc(Mid(Text1.Text, x, 1)) < 91 Then
iUpperCase = True
Exit For
End If
Next x

If (iNumber And iLowerCase) Or (iNumber And iUpperCase) Then
MsgBox ("Good Password")
Else
MsgBox ("Bad Password")
End If
End Sub

That's an awfully astounding

That's an awfully astounding column you’ve posted. San Diego Plumbing

String to Array of Character

How to convert string to array of characters?

string count

can somebody help me with my project? i need to create a simple system that can count anything that has been typed on textbox

example:

textbox1 (input): 123 asdb46./-76=8
textbox2 (output): 17

the input should be limited to 60 characters only, else it will display "more than 60 characters"

thanks! :)

Hope you got this

Private Sub Text1_Change()
If (Len(Text1.Text) >= 60) Then
MsgBox "60 Characters only accepted", vbCritical, Error
Text2.text=len(Text1.text)
End If
End Sub

VB 6.0

I should not be able to enter any other chaaracters other then numbers in the text box. Am working on VB6.. pls give me the suggestion..

Private Sub

Private Sub text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

hope it will help u..

Only accept numeric data for textbox control

Add control textbox name Text1 to your form
double click on that control, change the event to KeyPress. Copy and paste the code bellow.
Perhaps the code help for you...

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0") To Asc("9"), 1 To 31: KeyAscii = KeyAscii
Case Asc("."), Asc(","): KeyAscii = KeyAscii 'In case you are suppose to use formating numeric e.g. 1,234,567.89
Case Else
KeyAscii = 0
End Select
End Sub

storing each character from a textbox in an array

For i = 0 To 9 Step 1
password(i) = Text1.Text
Next
For i = 0 To 9 Step 1
text2.text = text2.text + password(i)
Next

here is the case i want each character entered in the textbox in an array........

String to Array

dim password() as byte
password = Text1.text

Dim i As Long
Text2.Text = vbNullstring
For i = 0 To UBound(password)
Text2.Text = Text2.Text & Chr(password(i))
Next i

Language translation problem

Hi....
I have a Access Database with Emp_Name_Eng field set to text shows the name of employee in English Language.
I want to add new field Emp_Name_Marathi which convert English name to Marathi Language.
If anyone have code for that please send me.

Or send me the code for Conversion of English characters to standard Unicode.

Please Help Me.

Language translation problem

Hi....
I have a Access Database with Emp_Name_Eng field set to text shows the name of employee in English Language.
I want to add new field Emp_Name_Marathi which convert English name to Marathi Language.
If anyone have code for that please send me.

Or send me the code for Conversion of English characters to standard Unicode.

Please Help Me.

Thank You!

I am doing part time BCA. My college notes suck big time. They don't even contain half of the popular and basic functions.
Had it not been for this tutorial on VB6, I wouldn't have managed to even get the basic idea about using VB6.
Thanks! You've been very helpful. : )

HELPPPPPPPPPPP

I have a sulution is:
I want convert string to more string by spcial word
Ex1: Input string to text box is"Test Test|" output is Test Test. But when is not spical "|" then output is "Test or Test"
Ex2: Input is type "Tes?t" then output is replace word "?" by one or nothing word.
Ex3: Input is type "Te_t" then output is replace word "_" by just one every word.

Sorry language English is not well.
Thank you very much

Replace

I know what you meant, its very simple...

Ex1: Input string to text box is"Test Test|" output is Test Test. But when is not spical "|" then output is "Test or Test"
Answer:
Exp1 = Replace("Test Test|", "|", "")

Ex2: Input is type "Tes?t" then output is replace word "?" by one or nothing word.
Answer:
Exp2 = Replace("Tes?t", "?", "",1,1)

Ex3: Input is type "Te_t" then output is replace word "_" by just one every word.
Answer:
Exp2 = Replace("Te_t", "_", "",1,1)

May Allah reward the vb info

May Allah reward the vb info provider with all. I am highly happy that I have gained a lot. I wish so in the future.

datagrid

how to validate data entered in fields of datagrid

Please a little help

I use this code:

Sub Transformare()
For i = 1 To Len(timp)
If Mid(timp, i, 1) = "." Then unu = i: Exit For
Next i
timp2 = Mid(timp, unu + 1, Len(timp) - unu)
For i = 1 To Len(timp2)
If Mid(timp2, i, 1) = "." Then doi = i: Exit For
Next i
doi = doi + unu
If Len(timp) - doi > 3 Then MsgBox "Timp introdus gresit!" & vbCrLf & "Verificati timpii!"
zecimi = Val(Mid(timp, 1, unu - 1) * 6000) + Val(Mid(timp, unu + 1, doi - 1 - unu) * 100) + Val(Mid(timp, doi + 1, Len(timp) - doi))
End Sub
the initial values are from mshflexgrid and are something like this: 2.35.58 time and the code is coverting to miliseconds. what i try to do is if one or more of the initial values are "ABANDON" then the code to skip that row. can someone help me?

Help please!

Hi to all
Please help me.I need urgent code for compare words in two lists in Excel and when one word is repeate in both lists then this word have to be coloured.
Thanks in advance to all

HELPPPPPPPPPPP ASAPPPPPPPPPPPPPPP!

HI! I WANT TO COMPARE MY TXBX1 TO MY TXBX2. IF THE CHARACTERS(EVEN RANDOMIZE) IN TXBX 1 IS DIFFERENT TO TXBX2 THEN THE ANSWER IN TXBX 3 IS NO IF ELSE YES. WHAT ARE THE CODES? ASAP! HELP PLS!

very easy. sort out

very easy.

sort out alphabetically each entry, then compare them to each others.

compare value in text boxes

quick answer coz i dont have time for implementation

Assume you that you have three text boxes:

if txtbox1.text=txtbox2.text then
textbox1.text=yes
else
textbox1.text=no
end if

try that

comparing textboxes

if textbox1.text=text2box.text then
textbox3.text="yes"
else
textbox3.text="no"
end if

reply

if strcomp(text1.text,text2.text)=0 then
text1.text="yes"
else
text1.text="no"
endif

program that only displays those strings begins with letter "b"

how to do a program that reads a series of strings from the user and displays only those strings beginning with the letter "b"

pls write to me statements

pls write to me statements in BASIC
(1) to replace two right characters of the string A$ by "IN"

I want to find the Number in String in VBA macros

Hi Can any one help me to find the number in Alphanumric string in VBA eg. if i hav 12abc then the output will be 12

VAL()

VAL()

find string that ends with a underscore

searching down a listbox for strings that end with an underscore_ .. not sure how to get it to work correctly,can anyone help please?

hope this helps.

'needed:
'text1.text = "text_"
'text2.text = ""
'command button
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Command1_Click()
If Right(Text1.Text, 1) = "_" Then
Text2.Text = "true"
Else: Text2.Text = "False"
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'hope this helps
Stop

convo game

i want to do a conversation with use of a lable and an iput box
the lable caption says " how are you?"
then the user answers writing in the input box..
i want to be able to determine what he writes sort of like this
if inputbox.text = good, or , fine, or , happy , or ... like using a buch of examples so as not to have to write
a ton of if then statements for each question that I ask the user.

Solution

You could declare the positive expressions and the negative expressions once and then call it over and over again. You could also divide them into other classes.
For example:

-----------------------------
Use this in a module:
-----------------------------

Public Function GetPositiveInput(what As Integer) As String

Select Case what
Case 1
GetPositiveInput = "good"
Case 2
GetPositiveInput = "great"
Case 3
GetPositiveInput = "fine"
End Select
End Function

----------------------------------
To call this, you could use:
----------------------------------

Private Sub Form_Load()
Dim x As String, y As Integer, z As String

x = InputBox("How are you?", "Question")
For y = 1 To 3
z = GetPositiveInput(y)
If x = z Then
MsgBox "lol.. Good day.."
Exit Sub
End If
Next y
End Sub

If you need anymore help contact me here: sakhawat21@gmail.com

Linking record cells

Please bear with me if this is a bit sketchy.
I have a php form that feeds back to a MySQL database. This is then accessed with an Access db.
On the php form I have a dropdown box that has a list of users. When a user selects one of these users it auto fills a linked cell on the form with the users First initial and Surname.
The data is sent along with other selections from the form to a single record in the MySQL db. I then copy the table that this data is held in so I have a backup of the original records and a working copy to make changes to.

My problem is here. If using the Access db to amend the records and a different user is selceted from the dropdown menu the linked cell in the record does not update. The table that populates the dropdown is called Exec_list and has headings Autonumber, Exec_name, email, Tel, Initialed_name.

What I need is to be able to link the dropdown list to the Initialed_name so that if anyone selects another user it updates the Initlialed_name section.

If you require anything else please contact me and I will see what I can do. This section affects about 3-4 other operations in my application so I need help.

Hope you can help in some way.

Dan

Thanks a lot

Thanks a lot for consolidating the string functions. This helped me in building a macro to generate random password.

Pls help

I need to create an application in vb6 to store my cd keys of various softwares. In the text box, i need to automatically add an hyphen(-) after every 5 characters as in usual cd keys. How do i do it n store it in a database. Pls help...

String Search & Replacement with another character

How to search for a character in string in reverse order and replace it with another character?

plshelpme

if we enter the word ESTABLISHMENT , first we check the last character T in the database table if we get it then it will count 1character, if it will not found in the database then leave it, then check the last 2character NT in the data base if found then count 2 character, like this count the whole word and check it in data base table, now we have the counts 1 character, 2 character,... so then replace/delete the largest count of character by the particular character which is present in the database.....
thanku

Sending a string through mscomm

Hello,
I would like to know how i can send a string like 4352119 out of the serial port at a baud rate of say 9600. How is it going to be sent? Are they going to be sent out individually, like 4 will be sent out followed by 3 and so on, with a start and stop bit for each one ? I'm using VB6
MSComm1.Output = "4352119"

Find string

Very new to VB. Need code to copy the string that follows "PMT INFO:TRN*1*" to the next * to a cell right beside this data field in excel. So would need 123456789 in the example below. The number of characters to return will vary and in some cases will not be present at all. The number of rows to search will also vary. Please help...

Payor Name DES:CUSTEFTS ID:XXXXX2061 INDN:Practice Name CO ID:1066033492 CCD PMT INFO:TRN*1*123456789*555555555- 44345 0341 12/30 UNPOSTED NO REASON. CN

Solution, Did not test, but the idea is correct


Dim NumberAs String
Dim Start As Integer
Dim End As Integer

' Select the cell that has the "PMT INFO:TRN*1*" tag,
objSpread.Application.Cells.Find(What:="PMT INFO:TRN*1*", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate

' The actual value is on this cell, but some formatting is required to strip the number
Start = InStr(objSpread.Application.Selection.Value, "PMT INFO:TRN*1*") + 15
End = InStr(Start, objSpread.Application.Selection.Value, "*")
Number = Mid$(objSpread.Application.Selection.Value, Start + 1, End - Start)

Debug.Print Number

help pls

in textbox i use this code
Private Sub Text1_Change()
' Allow only numbers."
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
If Not (ch >= "1" And ch <= "4") Then
' Cancel the character.
KeyAscii = 0
End If
End Sub
but i cand use delete or backspace....
pls help me i need to use the delete or backspace.????? thanks

Here's the solution

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > Asc("9") Or KeyAscii < Asc("0") And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub

It works 100%

Keytrapping

try the folowing

'declare this as const at declarations in form
Const vbKeyDecPt = 46

‘Only allow number keys, decimal point, or backspace
If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or KeyAscii = vbKeyDecPt Or KeyAscii = vbKeyBack Then
Exit Sub
Else
KeyAscii = 0
Beep
End If
End Sub

its easy it will work for sure

dis is my reply

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If

special keys access or catch from the Keydown event of textbox

You will use the KeyDown event to access the back space and delete or other special keys,....:)

try this Private Sub

try this

Private Sub Text1_Change()
If IsNumeric(Text1.Text) = False Then
Text1.Text = ""
End If
End Sub

help pls

in textbox i use this code
Private Sub Text1_Change()
' Allow only numbers."
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
If Not (ch >= "1" And ch <= "4") Then
' Cancel the character.
KeyAscii = 0
End If
End Sub
but i cand use delete or backspace....
pls help me i need to use the delete or backspace.????? thanks

Private Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
If Not (ch >= "1" And ch <= "4") Then
' Cancel the character.

If KeyAscii >= 32 Then ' checks for control keys
KeyAscii = 0
End If

End If
End Sub

Hope this helps : )

Hello... Anybody there to help me please

I would like to copy from a access table to another table ( two text value )

vertdb.Execute "insert into osdbox(clid,byval,sellval,qty,rate,hcp,phbyval,scode,scrip)values('" & rsm4!clid & "'," & rsm4!byval & "," & rsm4!sellval & "," & rsm4!qty & "," & rsm4!Rate & "," & rsm4!hcp & "," & rsm4!phbyval & ",'" & rsm4!scode & "'," & " '," & rsm4!scrip & "')"

the problem is before scrip(test value) ' is comming .

please give me the correct command to remove ' symbol from scrip

easy to use the sql command :) [Vinay]

vertdb.Execute "insert into osdbox values(select * from other table)"

keep in mind the both tables fields structures are same then the above command will work, be happy...
feel free to ask another question on my email guptavinay.1@gmail.com

2 know about string functions

i want 2 know a brief description about string functions....................................

Nope.avi

Sorry, can't do that. First, you must take the epic journey of learning to spell

How to avoid "&" character to interrupt string.

I have a VBA code which allows me to pick fields from a DB and send email.
It sounds like: Me.Email.HyperlinkAddress = "mailto:" & RecipientName & "<" & RecipientAddress & ">"
The problem is that if the RecipientName field has the "&" character in it, the string is truncated at that point.
I have tried to use Replace and Format functions with Chr(38) code, but without success.
How can I do? Thank you.

Test

Sub CheckTCNumber()
Dim Count As Integer
Dim Num As Integer
Dim LastRow As Integer
Dim TCString As String
Dim TCScenario As String
LastRow = ActiveSheet.UsedRange.Rows.Count
Num = 1
Count = 1
For rwNumber = 3 To LastRow

If ActiveSheet.Cells(rwNumber, 1).Value <> "" Then
If Count < 10 Then
TCString = "TC00" + Trim(Str$(Count))
Else
TCString = "TC0" + Trim(Str$(Count))
End If

ActiveSheet.Cells(rwNumber, 1).Value = TCString
Count = Count + 1
TCScenario = ActiveSheet.Cells(rwNumber, 6).Value
Mid$(TCScenario, 1, 5) = TCString
ActiveSheet.Cells(rwNumber, 6).Value = TCScenario
End If
Next rwNumber
MsgBox "Test case number checked"

End Sub

Help Me Please

Dear Friends

I m new in vb 2005. i want that

1. i have to enter a sentence to a text field and when i press a button then my given sentence will split into words and replace these words from mysql database

example: i have entered "i am fine and ok"

then "i" will be converted to "eye"

"am" to "m"

"fine" to "f9"

"and" to "n"

"ok" to "okay"

and this show sentence again "eye m f9 n okay" on text field

but these words and replacing words will be taken from mysql database

Please help me sir .......

regards :::: it eagle

if your database includes

if your database includes "firstword" and "secondword".. for example "I" and "eye"..
then you have to write down the whole database into an array.. like firstword() and secondword()

so you can just say: "

for i = 0 to count(firstword)

strString = replace(strString, firstword(i), secondword(i))

next
"

the result will be : "eye m f9 n okay"!

Very useful site

Hi......All
Thai is S.S.Sahoo. i am working as a software engg.
today i vought this site through google.
& I found that this is a best site for those who want to learn some thing.
And also i learnt a lot from this site........i will definitely use this site for learn more.& suggest you people to go through this site & spend some time ,if u r serious .
Thanks
Jipu

hi i want to search a name

hi i want to search a name which is of length aroung 40 character from a sql database using the recordset .
plz help me
i just needed it for my project

Thanks a lot for this

Thanks a lot for this information on VB string functions.

HELPPPPP

gr8 tutorial but i need some help...i want to manipulate each character separatly entered in the textbox....actually i want to rite a ceaser cipher algo in VB n so for that i have to consider each character separately...kindly help me out on how to do it....thx

Help with searching string functions

This site has shown me lots of shortcuts to string-processing procedures, but before that I used to use the following technique for processing any string or text:
'let's say the text is called; joetext
joetext = "Howdy buddies?"
'the length of the string is joelen
joelen = Len(joetext)
'and then comes addressing each character separately
For I = 1 to joelen
currentchar = Left$(Mid$(joetext, I), 1)
Next I
'now for each round of the loop the character would be stored in currentchar.

That way you can do whatever you want with each character inside the loop mentioned above!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options