How to reset the box - vb6

Using masked box in the form
masked1.mask = ##:##
In form load, masked1 display as __:__
Once user enter the values like 08:00 then reset means it should display again like this __:__
How to do this?

To clear a MaskEditBox you set the Text property to an empty string, however when the PromptInclude property is True you'll get an error. I would suggest writing a Sub method that you can call when you want to clear it.
Private Sub ClearMaskedEditBox(ByVal vMaskEditBox As MaskEdBox)
Dim strMask As String
strMask = vMaskEditBox.Mask 'save the current mask
vMaskEditBox.Mask = "" 'clear the control's mask
vMaskEditBox.Text = "" 'clear the text
vMaskEditBox.Mask = strMask 'reset the mask
End Sub
To use you call the Sub with the MaskEditBox control you want to clear.
Call ClearMaskedEditBox(masked1)

Related

Proper Casing in VB6

I am doing a quiz game in VB6. I need the textbox to automatically capialize the first letter but this code
Private Sub Anstxt_Change()
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
End Sub
causes the word to invert. So instead of "Trees" it turns into "Seert"
How do I change this?
Pay attention to where the cursor is positioned in the textbox when the event Change occurs: its at the start of the textbox. Add a Debug.Print statement to see what's going on while you type:
Private Sub Anstxt_Change()
Debug.Print StrConv(Anstxt.Text, vbProperCase)
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
End Sub
The output looks like
T
T
Rt
Rt
Ert
Ert
Eert
Eert
Seert
Seert
Two things to notice here: the Change event is triggered twice: once from typing and once from changing the value of the textbox within the Change event. That gives you an idea that manipulating the text of a textbox in its Change event isn't a good idea. I suggest to put this code in the LostFocus event instead.
The second thing to notice is that as the cursor is always at the start of the textbox, the letters you type are inserted there in front of the existing letters. So after you change the .Text property of the textbox, you should position the cursor at the end of the textbox with the .SelStart method:
Anstxt.SelStart = Len(Anstxt.Text)
e.g.
Private Sub Anstxt_Change()
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
Anstxt.SelStart = Len(Anstxt.Text)
End Sub

Unbound imagecontrol ONLY show First image when imagelink is added in a 1:N relation

Background:
Entry is via a subform for adding/showing/linking images.
I do not want to store the image files within my DB, the image folder is separate. The DB will grow rather large in time.
I have created a click-control enabling a popup for user to browse and click on the imagePATH to be added in a Bound Textfield (called Bildadress, no not misspelled in My country, Grin ) in the subform.
See code below.
Then I add a new unbound Image-control and specify its Controlsource = the Textfield mentioned above.
For the firs image this works wonderful, but for the following the Image-control returns NULL (not show att all). The data in the Textfield updates as it should.
Will the 2nd stage only work in a 1:1 relationship OR can I (with your help) use VBA code to make this work?
OPTIMAL would be to get this to work and also a 2nd Bound Textfield just displaying the actual image file name. .
I hope someone out there have encountered this problem who also didnt want to use Attachment to store the files within the databae.
CODE:
Private Sub AddFilePath_Click()
Call Selectfile
End Sub
Public Function Selectfile() As String
Dim Fd As FileDialog
Set Fd = Application.FileDialog(msoFileDialogOpen)
With Fd
.AllowMultiSelect = False
.Title = "Välj önskad fil"
If .Show = True Then
Selectfile = .SelectedItems(1)
Me.Bildadress = Selectfile
Else
Exit Function
End If
End With
Set Fd = Nothing
End Function
If you use a bound textbox that holds the image-path then you can use
Me.Imagecontrol.Picture = Me.BoundTextControl.Value
to load the picture into an unbound image control. In your case that would be something like
If .Show = True Then
Me.Bildadress.value = .SelectedItems(1)
Me.Bild.Picture = Me.Bildadress.value
Else
It would be best to also load the respective picture in the OnCurrent Event.
Private Sub Form_Current()
Me.Bild.Picture = Me.Bildadress.value
End Sub
However, keep in mind that access is a one-file-database and you break that paradigm when using links to external files where the files would belong into the DB.

vb6 use same button on 2 textbox

Hi our instructor asked us to create a calculator that only uses button
for the value input in, It should have enter value1 on 1st textbox and then the other value on the next textbox in vb6
is there a way to use the same button to enter a value on the next textbox?
lets say after you press button 3 it will show on textbox1
Text1.Text = "3"
my problem is it wont go to the next textbox after it show the number 3
I've already tried
If Text2.Setfoucs = True Then
Text2.Text = "3"
Else
Text1.Text = "3"
End If
Its giving me error.
I just wanted to use the same button on the second textbox
after it was used on the 1st textbox
I thought of using another bunch of buttons and set visible = true after the
1st button was pressed so that the nextone will be
Text2.Text = "3"
Im just a beginner on VB6 any suggestions will be greatly appreciated.
here is what the project looks like
http://i.imgur.com/ixK9s1U.png
setFocus is a function, not a variable, and it doesn't return a value, so you can't use it in an if clause.
Here is my suggestion to accomplish what you're trying to do:
Add a GotFocus event to each of your textboxes, that sets a variable. Like so:
Private selectedTxtBox As Integer
Private Sub Text1_GotFocus()
selectedTxtBox = 1
End Sub
Private Sub Text2_GotFocus()
selectedTxtBox = 2
End Sub
Then on your button, you can do:
If selectedTxtBox = 1 Then
Text1.Text = "3"
ElseIf selectedTxtBox = 2 Then
Text2.Text = "3"
End If

Excel VBA: end user select image on computer and submit on user form

I have a userform with a bunch of textboxes. End user inputs test data into form and after clicking "Submit" button, this info is saved to an excel sheet. I am wondering how to make it possible for user to input images from their computer into the form.
Requirements: End user be able to select an image from their computer and click submit on the form to have it inserted into an excel sheet.
Private Sub CommandButton3_Click()
Dim image As FileDialog
Set image = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedPicture As Variant
With image
If .Show = -1 Then
For Each vrtSelectedPicture In .SelectedItems
'Show path in textbox
TextBox71.Text = .SelectedItems(1)
Next vrtSelectedPicture
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing
Set image = Nothing
End Sub
Sure, here is a sample code that may give you an idea about FileDialog.
Private Sub CommandButton1_Click()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.ButtonName = "Submit"
.Title = "Select an image file"
.Filters.Add "Image", "*.gif; *.jpg; *.jpeg", 1
If .Show = -1 Then
' file has been selected
' e.g. show path in textbox
Me.TextBox1.Text = .SelectedItems(1)
' e.g. display preview image in an image control
Me.Image1.PictureSizeMode = fmPictureSizeModeZoom
Me.Image1.Picture = LoadPicture(.SelectedItems(1))
Else
' user aborted the dialog
End If
End With
End Sub

textbox validation in vb6 (disable paste option)

validation on textbox in vb 6.0
i tried
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (KeyAscii = vbKeyBack Or KeyAscii = vbKeyDelete Or KeyAscii = vbKeySpace Or (KeyAscii >= Asc("1") And KeyAscii <= Asc("9"))) Then
KeyAscii = 0
End If
but now i want that paste option should disable when i right click on textbox
If you want to prevent right-click menu items from being used on the textbox you can create your own context menu.
Then, using API calls you need to unhook the default menu from the textbox and hook up the custom menu.
(I'm not aware of other APIs which only let you disable/hide items in the existing context menu)
The downside off course is that any menu item you want to keep such as copy or delete you need to write the code for yourself.
You can find a very good explenation on how to do this here Disable right click menu inside a textbox and here Weird reaction on a popup menu
What next,.. what is if the user uses CTRL+V to paste? Or what if the user has paste mapped to different key combinations, other than CTRL+V?
Validate the data instead?
You can end up writing a lot of code trying to prevent data entry. Why not save that work and instead let the user enter what they like and use the validate event to validate the data?
I wrote a sample on another site on using the validate event of a textbox here: Validate Value Is Numeric. That link also has a vb6 demo project I put together attached in the post.
The type of validation is irrelevant I suppose, it simply demonstrates using the validate event allows you to focus on validating the data, rather than trying to code every single possible way you can think of trying to prevent data to be entered in the first place.
The Validate event is triggered before lostfocus and before the next control's getfocus.
Only if the validate event is not told to cancel the action, will the lostfocus event and any subsequent event be executed.
The Validate event is intended to be used to ensure the control's value is validated before executing any other event.
Private Sub Text1_Change()
If Text1.Tag <> Text1.Text Then
Text1.Text = Text1.Tag
Text1.SelStart = Len(Text1.Text)
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Text1.Tag = Text1.Text & Chr(KeyAscii)
End Sub
Old problem but still relevant with legacy projects.
The solution proposed by Sam Sylvester is interesting but doesn't allow you to delete characters (use backspace) and appends the 22 ASCII character (Ctrl + V) in the textbox (which is bad).
This solution I found worked for me (prevents paste using Ctrl + V and also using the context menu):
Dim preventPasteFlag As Boolean
Private Sub Text1_Change()
If preventPasteFlag Then
Text1.Text = Text1.Tag
preventPasteFlag = False
Else
Text1.Tag = Text1.Text
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 22 Then
preventPasteFlag = True
End If
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
preventPasteFlag = True
End If
End Sub
this is quite similar:
Private Sub Text1_Change()
Dim i As Long, sTemp As String
For i = 1 To Len(Text1)
If InStr(1, "0123456789", Mid$(Text1, i, 1)) > 0 Then sTemp = sTemp & Mid$(Text1, i, 1)
Next
Text1 = sTemp
End Sub

Resources