How to save the text box values into an array? - vb6

I need to enter values in text box and when ever i press enter i have to save the text box values into an array.How can i achieve this functionality?

I would use a separate counter variable to redefine the size of the array, like this:
Option Explicit
Dim myArr() As String '~~~ dynamic array
Dim lngCnt As Long '~~~ a counter variable that keep track of the index
' inital stage..
Private Sub Form_Load()
lngCnt = 0
End Sub
' on KeyPress
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then '~~~ if Enter Key is pressed..
ReDim Preserve myArr(lngCnt) '~~~ reclare the array with the new size, while preserving any elements it may contain
myArr(lngCnt) = Text1.Text '~~~ store the line
Text1.Text = "" '~~~ empty the textbox, so that you could type the next line
lngCnt = lngCnt + 1 '~~~ increment the counter, which we would use as size during the next keypress
End If
End Sub
' to display the elements
Private Sub Command1_Click()
Dim i As Long
For i = LBound(myArr) To UBound(myArr) '~~~ loop through the elements(from Lowerbound to Upperbound)..
Debug.Print myArr(i) '~~~ ..and display the item.
Next
End Sub

If you just want a new entry to be added each time they press the button, then use something like:
Redim Preserve YourArray(LBound(YourArray) To UBound(YourArray) + 1)
YourArray(UBound(YourArray)) = TextBox.Text
Note that this can get very slow and inefficient when the array contains large numbers of items as it's reallocating memory each time.
A better method would involve expanding the size of the array in chunks, while keeping track of the last valid entry.

Just give same name to all the text Boxes

The short answer is to use split to break the string up (you will need to tell the user what char to use to split on).
Long answer don't change the user interface to a repeater and have them use an input per value start here http://blogs.microsoft.co.il/blogs/basil/archive/2008/08/20/javascript-repeater-control-datarepeater-using-jquery-presenter-1-0-8-uicontrols-library.aspx
Else you will be debugging for ever.

Related

VB Control Naming by Variable

Dim LineNo as Integer
LineNo = CStr(channel) 'This can have a value of 1 to 100
If LineNo = 1 then
Text1.Text = "Line one selected"
Elseif LineNo = 2 then
Text2.Text = "Line one selected"
'Etc etc
End if
I need to replace the number "1" in Text1.Text and every other TextBox with the value of LineNo? For example:
Text{LineNo}.Text
So I would not have to do a repeated "If" and have a smaller one line code like this:
Text{LineNo}.Text = "Line " & LineNo & " selected"
How would I do this?
Look into a Control array of text boxes. You could have txtLine(), for example, indexed by the channel number.
LineNo = CStr(channel)
txtLine(channel).Text = "Line " & LineNo & " selected"
To create the array, set the Index property of each of the text boxes to an increasing integer, starting at 0.
If you have a finite and relatively small number, you can use a property. I've used this approach with up to 30+ elements in a pinch. Super simple, easy pattern to recognize and replicate in other places. A bit if a pain if the number of elements changes in the future, but extensible nevertheless.
It uses the Choose statement, which takes an index N and returns the Nth element (1-based), hence, the check makes sure that N is > 0 and <= MAX (which you would configure).
Public Property Get TextBox txt(ByVal N As Long)
Const MAX As Long = 10
If N <= 0 || N > MAX Then Exit Property ' Will return a "Nothing". You could return the bound element if you prefer
set txt = Choose(Text1, Text2, Text3, Text4, Text5, Text6, Text7, Text8, Text9, Text10)
End Property
Then, you can simply reference them with the Property, much like an alias:
txt(1).Text = "Line 1 text"
txt(2).Text = "Line 2 text"
If you have an arbitrary number, then you are likely using a control array already, which is simpler because it can be referenced by Index already, so you can directly reference it.
If neither of these work for you and you have a very large number of controls, you can scan the Controls collection in a similar Property, attempting to match ctrl.Name with the pattern of your choice (e.g., matching the first 4 characters to the string "Text", thus matching Text1, Text2, etc, for an unlimited number). Theoretically, this should be future-proofed, but that's just theoretical, because anything can happen. What it does do for you is to encapsulate the lookup in a way that "pretends" to be a control array. Same syntax, just you control the value.
Public Property Get TextBox txt(ByVal N As Long)
Dim I As Long
For I = 0 To Controls.Count - 1 ' Controls is zero-based
' Perform whatever check you need to. Obviously, if you have a "Label" named
' "Text38", the assignment will throw an error (`TextBox = Label` doesn't work).
If Left(Controls(I).Name, 4) = "Text" Then
set txt = Controls(I)
End If
Next
' If you want the Property to never return null, you could uncomment the following line (preventing dereference errors if you insist on the `.Text` for setting/getting the value):
' If txt Is Nothing Then Set txt = Text1
End Property
Use the same way as above: txt(n).Text = "..."

Is there a way to associate data with an MSFlexGrid cell that is not displayed to the user?

Say I've got your standard MSFlexGrid. I have an image in a cell that already has a tooltip.
What I want to do is store a string about the image in the cell that's associated with the cell but is not displayed to the end user.
I've been looking at the properties on the MSDN website but I didn't see anything that would work.
Is this possible? Has anyone done something similar before? I'm not opposed to using properties that were not necessarily designed for data storage in this sense provided they're not being used already, but obviously this is not desirable.
So here's what I did for those who may be looking to do the same. It's not elegant, it's not pretty, but it works.
Private m_colImageInfo As Collection ' dictionary to store image name Key="Index^m_Tracker Row^Column" Value=IconKey
Private m_colImageInfoSorted As Collection ' Dictionary to store image name Key="Index^FG Row^Column" Value=IconKey
Private m_colSortRowInfo As ACCollection ' How the row changes
I have a function, called DoDisplay() that gets called everytime the grid is refreshed or initially displayed.
In it, I clear the m_colSortRowInfo Collection (ACCollection is just a wrapper we've built).
Call m_colSortRowInfo.Clear ' Note ACCollection allows the use of Clear
Now in do display we're grabbing data from the model, and get the actual image with the first DoAction call, and then get the image key (unique identifier about the image) with the second call.
Set oImage = m_Tracker2.DoAction(TS_ACTION_GET_COL_ICON, lRow, lColumn, False) 'Retrieve the icon image ' get the image
sString = m_Tracker2.DoAction(TS_ACTION_GET_COL_ICON, lRow, lColumn, True)
If Len(sString) > 0 Then
On Error Resume Next
Call m_colSortRowInfo.Add(CStr(.Index & "^" & lRow & "^" & .Col)) ' lets store the old key as a value for later access
Call m_colImageInfo.Add(sString, CStr(.Index & "^" & lRow & "^" & .Col))
On Error GoTo 0
End If
Then I add the icon key to m_colImageInfo as the Collection's "item" and the key being the grid Index^m_TrackerRow^Column.
To the m_colSortRowInfo I set the the same value (Index^m_TrackerRow^Column) as the item and then the key is just a simple iterative index.
Now the problem I was encountering was that these images exist in the grid, but I don't know anything about them besides that the cell contains, or doesn't contain an image / text. I need to associate the icon key with the image for reasons irrelevant to this post.
So now, we've gotten the grid, row and column of the model that the image is in, but we don't know where it actually lies in the grid. The column stays the same, but based on how the user has sorted the grid, the rows can change. This is the key challenge.
We have a way to map the model grid row to a specific MSFlexGrid row. I use that in the method below to populate m_colImageInfoSorted, which is the actual representation of each image in the grid.
'------------------------------------------------------------------
' NAME: SortImageCollection (PRIVATE)
' DESCRIPTION: When we first create the flex grid we pull the images from
' the image provider service and scale them up unfortunately there is
' no reference to the image that gets back. So what we do is create a
' collection to keep track of the image location and then send it to
' the printing class. If the grid has been sorted however, we need to
' identify where that picture moved too. This sub serves that purpose.
' KEYWORDS: sort, icon, collection, image
' CALLED BY: DoDisplay
' PARAMETERS:
' Index (I,REQ) - FlexGrid index
' RETURNS: nothing
' ASSUMES:
' SIDE EFFECTS: populates m_colImageInfoSorted with image keys in the fg grid
' with index passed to the function
'------------------------------------------------------------------
Private Sub SortImageCollection(Index As Integer)
Dim lCnt As Long
Dim lNumImages As Long
Dim lColumn As Long
Dim lRow As Long
Dim lOldRow As Long
Dim lCurGrid As Long
Dim sTempIconStr As String
Dim sOldKey As String
Dim sNewKey As String
Set m_colImageInfoSorted = Nothing
Set m_colImageInfoSorted = New Collection
With fgFlexGrid(Index)
For lNumImages = 1 To m_colSortRowInfo.Count ' we will loop over all the images
lCurGrid = CLng(Piece(m_colSortRowInfo.ItemVar(lNumImages), "^", 1, False)) ' if the grid is the grid we we asked for, then continue
If (lCurGrid = Index) Then ' we are just sorting images for this grid
For lCnt = 1 To m_lPatientRows(Index)
On Error Resume Next
lOldRow = CLng(Piece(m_colSortRowInfo.ItemVar(lNumImages), "^", 2, False)) ' get the m_tracker row we stored earlier
lRow = getRowFromFlexGrid(Index, lCnt) '
sOldKey = m_colSortRowInfo.ItemVar(lNumImages) ' get the old key
lColumn = Piece(sOldKey, "^", 3, False)
sTempIconStr = m_colImageInfo.Item(CStr(Index & "^" & lRow & "^" & lColumn)) '
sNewKey = CStr(.Index & "^" & lCnt & "^" & lColumn) ' get the column, add to new key
If (Len(sTempIconStr)) > 0 Then ' if we have an image (didn't already remove it)
Call m_colImageInfoSorted.Add(sTempIconStr, sNewKey) ' Add the new image location
End If
On Error GoTo 0
Next lCnt
End If
Next lNumImages
End With
End Sub
And wahlah, the call to the sub is all it takes:
Call SortImageCollection(Index)
I know that the SortImageCollection() sub is not the most efficient. If there is a better way to do this mapping, let me know, I'm interested in the response, but this is working well, and has no apparent slow down from an end user perspective so I'm happy with it.

Vb6 .text property for textbox required

I am trying to convert letters to numbers.
I have a sub which ensures only numbers are put into the textbox.
My questions is will the following code work. I have a textbox(for numbers) and combobbox(for letters)
Dim sha As String
Dim stringposition As Long
Dim EngNumber As Long
sha = "abcdefghifjklmnopqrstuvwxyz"
stringposition = InStr(1, sha, Mid(1, cmbEngletter.Text, 1))
MsgBox "stringposition"
EngNumber = (txtManuNo.Text * 10) + stringposition
My only question above would be will the multiplication work with a .text. I believe it won't because it is a string. Please advise then on how to deal with a situation.
You can use CLng() to convert a string to a Long variable
CLng() will throw an error though if it doesn't like the contents of the string (for example if it contains a non-numeric character), so only use it when you are certain your string will only contain numbers
More forgiving is it to use Val() to convert a string into a numeric variable (a Double by default)
I also suggest you look into the following functions:
Asc() : returns the ASCII value of a character
Chr$() : coverts an ASCII value into a character
Left$() : returns the first characters of a string
CStr() : convert a number into a string
I think in your code you mean to show the contents of your variable stringposition instead of the word "stringposition", so you should remove the ""
I do wonder though what you are trying to accomplish with your code, but applying the above to your code gives:
Dim sha As String
Dim stringposition As Long
Dim EngNumber As Long
sha = "abcdefghifjklmnopqrstuvwxyz"
stringposition = InStr(1, sha, Left$(cmbEngletter.Text, 1))
MsgBox CStr(stringposition)
EngNumber = (Val(txtManuNo.Text) * 10) + stringposition
I used Val() because I am not certain your txtManuNo will contain only numbers
To ensure an user can only enter numbers you can use the following code:
Private Sub txtManuNo_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyBack
'allowe backspace
Case vbKey0 To vbKey9
'allow numbers
Case Else
'refuse any other input
KeyAscii = 0
End Select
End Sub
An user can still input non-numeric charcters with other methods though, like copy-paste via mouse actions, but it is a quick and easy first filter

functioning with objects of word document

I need to place three objects on my .doc in this order:
One Picutre;
Some Text;
One Table;
I recently learned how to place the image where I want (top of the doc).
But now, the table is getting in the middle of the text, how may I set the text with something like Position Absolute and then the Table below the text ?!
My Currently code:
Private Sub Command1_Click()
Dim Word_App As Word.Application
Dim Word_Doc As Word.Document
Dim Word_Table As Word.Table
Dim Word_Range As Word.Range
Dim iCount As Integer
'Insert the image
Word_App.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Word_App.Selection.InlineShapes.AddPicture FileName:="C:\p\53.jpg", SaveWithDocument:=True
Word_App.Selection.TypeParagraph
With Word_App
'Here I place some text
End With
'Insert Table
Set Word_Table = Word_Doc.Tables.Add(Range:=Word_Doc.Range(Start:=20, End:=20), NumRows:=3, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed)
Word_Doc.SaveAs FileName:="C:\p\TestandoPicture"
Set Word_Table = Nothing
Set Word_App = Nothing
Set Word_Doc = Nothing
End Sub
Here is an example of the result:
Notice that: In my code, I typed the position for my table Start:=20, End:=20 and it's in the 20th position of character... But i'd like to place it below the text... Wich is the best way to do so ?
Highlight the text, do a word count on the highlighted text, then use the character count from that to position your table. Crude but effective.

How to limit the value of the textbox in vb 6.0

I am doing a marks updating database system. I need to limit each of my textbox to be in the value of less than 100, when its over than 100 or when its not number, a message box will pop up and the data won't be save until the user change the mistake. How can I do it?
I agree with Hiren Pandya, but I thought I would add my own take as well.
Be aware that converting a string to a numerical value is not trivial, but the Val, CInt, CDBl etc. functions in VB6 can all give you behavior that close to what you want. (some of those links are for VB.Net, but can still be valuable). You want to make sure you are thinking about digit grouping, positive/negative, decimal separators, etc. when you are validating user input on your own. Most of the time, the built-in functions are good enough.
Private Sub Text1_Change()
On Error GoTo Err_Handler
Dim text As String
text = Text1.text
If IsNumeric(text) = True Then
'If you only want integers...
Dim value As Integer
value = Val(text)
If value <= 100 And value > 0 Then
'The value is good so whatever stuff you need to do
'And then leave the procedure
Exit Sub
End If
End If
'Let everything else fall through here...
Err_Handler:
MsgBox "Invalid input."
'Other stuff to prevent saving
End Sub
In the properties of the text box, set MaxLength to 2.
If you want a message, in the text box Change event, you could do...
If Len(txtBox.Text)>2 then msgbox...
then add your message in the messagebox.
I could go into more detail if you need it. Some thing like below...
Private Sub Text1_Change()
If Len(Text1) > 6 Then
Text1 = " "
MsgBox "Not more than six"
Text1.SetFocus
End If
End Sub

Resources