Using Array :
strmsg = "John Doe der"
Dim a() As String
a = Split(strmsg, " ")
Debug.Print a(UBound(a))
Using Array :
strmsg = "John Doe der"
Dim a() As String
a = Split(strmsg, " ")
Debug.Print a(UBound(a))
First you must know what the meaning of the error warning.
"Runtime error '381 'Invalid property array index".
You said that vb pointing you in line 2 (List2.Selected(i) = True
) when error is came up, it means that your selected index is not available on the list.
So what the problem here?
Provides more information.
Thanks
This should working :
Private Sub Command1_Click()
xSearch = Text1.Text
For i = 0 To Grid.Rows - 1
If xSearch = Grid.TextMatrix(i, 1) Then
Grid.Row = Grid.TextMatrix(i, 1)
For j = 1 To Grid.Cols - 1
Grid.Col = j
Grid.CellBackColor = vbCyan
Next j
End If
Next i
End Sub
This an example about struct of employee combining with array
Module1
Public Type Employee
EmpNo As Integer
EmpName As String
EmpPhone As String
End Type
Public Sub SetEmployee(ByRef emp() As Employee, ByVal i As Integer)
ReDim emp(i)
End Sub
Public Sub SetEmpData(ByRef emp() As Employee, ByVal i As Integer, ByVal no As Integer, ByVal name As String, ByVal phone As String)
emp(i).EmpNo = no
emp(i).EmpName = name
emp(i).EmpPhone = phone
End Sub
'GetEmpData will return array containing employee details
Public Function GetEmpData(ByRef emp() As Employee, ByVal i As Integer) As Employee
GetEmpData = emp(i)
End Function
Form1
All inputs will use inputbox and just add two button for set and get employee details
Dim NewEmp() As Employee ' declare array as struct of employee
Dim i As Integer
Private Sub Command1_Click()
' how many employee to add
temp = InputBox("How many employee ?")
SetEmployee NewEmp, Int(temp - 1)
' add employee detail
For i = 0 To UBound(NewEmp)
EmpNos = InputBox("Emp " & i + 1 & " No")
EmpNames = InputBox("Emp " & i + 1 & " Name")
EmpPhones = InputBox("Emp " & i + 1 & " phone")
SetEmpData NewEmp, i, EmpNos, EmpNames, EmpPhones
Next
End Sub
Private Sub Command2_Click()
' Extract detail of employee for each array
For i = 0 To UBound(NewEmp)
MsgBox GetEmpData(NewEmp, i).EmpNo & "," & GetEmpData(NewEmp, i).EmpName & "," & GetEmpData(NewEmp, i).EmpPhone
Next
End Sub
Or you can do this way :
Add Class to your project, named ListViewColumnSorter (You can modify it).
Replace with this following code :
Imports System.Collections
Imports System.Windows.Forms
Public Class ListViewColumnSorter
Implements System.Collections.IComparer
Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitiveComparer
Public Sub New()
' Initialize the column to '0'.
ColumnToSort = 0
' Initialize the sort order to 'none'.
OrderOfSort = SortOrder.None
' Initialize the CaseInsensitiveComparer object.
ObjectCompare = New CaseInsensitiveComparer()
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim compareResult As Integer
Dim listviewX As ListViewItem
Dim listviewY As ListViewItem
' Cast the objects to be compared to ListViewItem objects.
listviewX = CType(x, ListViewItem)
listviewY = CType(y, ListViewItem)
' Compare the two items.
compareResult = ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text, listviewY.SubItems(ColumnToSort).Text)
' Calculate the correct return value based on the object
' comparison.
If (OrderOfSort = SortOrder.Ascending) Then
' Ascending sort is selected, return typical result of
' compare operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Descending) Then
' Descending sort is selected, return negative result of
' compare operation.
Return (-compareResult)
Else
' Return '0' to indicate that they are equal.
Return 0
End If
End Function
Public Property SortColumn() As Integer
Set(ByVal Value As Integer)
ColumnToSort = Value
End Set
Get
Return ColumnToSort
End Get
End Property
Public Property Order() As SortOrder
Set(ByVal Value As SortOrder)
OrderOfSort = Value
End Set
Get
Return OrderOfSort
End Get
End Property
End Class
In your form :
Public …
No luck?
How about post your cmdquit codes.
It will help..
I have tried some but it will not work as I will
If I press the X button on titel bar then
I will it goes to cmdquit where I have to
choose vbyes or vbno.
The tests I haave done just quit the program
at once without any chance to choose.If vbyes is true then call cmdquit where
it make some backup files.
If vbno then exit sub
As i said in your previous thread (actualy same question)
It much easier to put your backup codes in procedure or function. you can call it in your cmdquit button event or when you press X button or any action when you needed to backup your files.
Hope it helps.
I just wonder how to enabled the X in the right
upper corner of a form.
What you mean about x mark in the right upper corner? Are you put a button or some other control there?
When I press the X then I will that it goes to cmdquit
and the program is ending.
I don't know why you need make it goes to cmdquit again since you can close your program when you click on X mark (button or any other controls). Like this following code :
Private Sub btnX_Click()
End
End Sub
But if you want to make it call cmdquit then here is the way.
I assume the x mark as button (but you can use another controls).
Also i don't know what it's in cmdquit but you said about ending the program.
Private Sub cmdquit_Click()
End
End Sub
Private Sub btnX_Click()
' This line of code will call
Call cmdquit_Click
End Sub
Or you mean about close button (X) on form title bar?
If yes then you can use form unload event :
Private Sub Form_Unload(Cancel As Integer)
Call cmdquit_Click
End Sub
Make autocomplete of combobox is different thing with prevent user input in combobox.
You drag us with preventing user input but actualy you want to make autocomplete of combobox.
VB6 and VB.Net has different way to make autocomplete. Autocomplete is an easy task in vb.net.
Here is the vb.net ways :
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MySource As New AutoCompleteStringCollection
MySource.AddRange(New String() {"Monday", "Thursday", "Wednesday", "Tuesday", "Friday"})
ComboBox1.AutoCompleteCustomSource = MySource
ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest
ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
End Sub
Try this :
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
If CType(sender, ComboBox).FindString(CType(sender, ComboBox).Text, 1) = True Then
'if it doesnt exist then remove the last character/s that dont match and set the cursor position to the end
CType(sender, ComboBox).Text = CType(sender, ComboBox).Text.Substring(0, CType(sender, ComboBox).Text.Length - 1)
CType(sender, ComboBox).SelectionStart = CType(sender, ComboBox).Text.Length
e.Handled = True
' accepting backspase
If Asc(e.KeyChar) = 8 Then
e.Handled = False
End If
End If
End Sub
You're welcome.
Don't forget to mark this thread as solved
Happy coding :)
You will get this error because using of Val() function.
e.g : Val(txtbox_tolagoldweight.Text * 96)
, you just multiply string with number.
try this :
Sub kaat()
goldkaatinratti = (((Val(TextBox1.Text) * 96) + (Val(TextBox2.Text) * 8)) + (Val(TextBox3.Text) * 8)) / 96
End Sub
do you know how to make pause button? i already try to make it after i click play button the song will not continue but it will be restart. this is my code for pause button.
You need to check what is the wmp state. Simple logic here. If state is pause then play the current song else play the selected song.
This is the modification of PlaySong Procedure :
Private Sub PlaySong()
If ListBox1.Items.Count > 0 Then
If Player.playState = WMPLib.WMPPlayState.wmppsPaused Then
Player.Ctlcontrols.play()
Else
Player.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
flag = True
End If
End If
End Sub
the same logic applies to the pause button. If state is pause then play the current song else pause the song.
Here is the code:
Private Sub Pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pause.Click
If Player.playState = WMPLib.WMPPlayState.wmppsPaused Then
Player.Ctlcontrols.play()
Else
Player.Ctlcontrols.pause()
End If
End Sub
Hope it solve your problem
Thanks mate! I have more questions stay tuned.
Don't forget to mark this thread as Solved.
More question? Just make new thread :)
Look at line 7:Result= Sale*0,04
It must be :Result= Sale*0.04
Also Commition variable is not used. Just remove it.
Seems right.
Okay.. We need boolean variable here to handle what section now.
' Declare flag as global variable
Dim flag As Boolean = True
...
Private Sub PlaySong()
AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
flag = True
End Sub
Private Sub NextSong()
If flag = True Then
If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
ListBox1.SetSelected(ListBox1.SelectedIndex + 1, True)
AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
End If
End If
End Sub
Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click
PlaySong()
End Sub
Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
flag = False
AxWindowsMediaPlayer1.Ctlcontrols.stop()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
NextSong()
End Sub
I have tried all posted code and works fine to me.
If you want to use timer then don't use playedstate event.
Also your if-then condition is different with my code above.
Read it carefully.
I prefer to use timer control to play next song.
It's hard to do it if you don't know the exact time when the file is saved.
About this case, i think better to have form to manage backup files, so you can choose any backup files to be deleted.
I fixed it
If you already fix it then please share with us the solution. Your answer will help another members who has same problem like you. Also make this thread as Solved.
Thank you.
ListBox1.SetSelected(ListBox1.SelectedIndex + 1, True)
This line will set selected item on listbox with the index of item.
I use Listbox.SelectedIndex + 1
as a new index.
The current index of playing song is selected item, to get index of next item we need to add current index with 1.
Set as True to make the item with an index is selected.
More info about Listbox.SetSelected(index as Integer,value as Boolean)
Also you can use another approach to play next song.
You don't have to use PlayStateChange event of Windows Media Player but you can use timer control. Checking if song is ended and play next song.
Just add timer to form, set Enabled = True and Interval to 1.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
ListBox1.SetSelected(ListBox1.SelectedIndex + 1, True)
AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
End If
End Sub
Hope it helps you to solve your problem.
To play next song :
Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
ListBox1.SetSelected(ListBox1.SelectedIndex + 1, True)
AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
End If
End Sub
Sorry, I mean this line :AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
Try :playlist.Items.Add(TextBox1.Text & "\" & fileInFolder.Name)
You still do the same thing.
You do not include the form file.
You're welcome.
Don't forget to mark as solved.
As i said before that we can't determine which file in position 4.
But the files always be arranged in ascending order by they name.
Say the number of files is 4 and you want to delete the file in postion 4 (the last file).
I try to put the files in listbox and accessing it by list index.
Private Sub Form_Load()
Dim strPath As String
Dim strFile As String
Dim FileCount As Integer
FileCount = 0
strPath = App.Path & "\backup\backup
strFile = Dir(strPath & "*.mdb")
Do Until strFile = ""
FileCount = FileCount + 1
List1.AddItem strPath & "\" & strFile
strFile = Dir
Loop
MsgBox FileCount & " file(s)"
End Sub
Private Sub Command1_Click()
Kill List1.List(List1.ListCount - 1)
End Sub
How you named this file?
That codes just count how many .mdb files and we don't know which file in position #4.
Actualy the Dir Function will listed and arrange the file in ascending type and order by they name.
E.g : you want to count mdb files in your app.path folder
Dim strFile As String
Dim FileCount As Integer
FileCount = 0
strFile = Dir(App.Path & "*.mdb")
Do Until strFile = ""
FileCount = FileCount + 1
strFile = Dir
Loop
MsgBox FileCount & " file(s)"
You can use this following code :
Private Sub Form_Load()
If Dir$("D:\dani.txt", vbNormal) = "" Then
MsgBox "This is your first time"
Else
' Do anything if text file exist
End If
End Sub
This backup files saved in one folder?
I have tried that to but now it stops at line 18
with follow error path/file accsess error 75
It happen because when you press a cancel button there are no filename of text file to return.
Filename needed in line 18 to read the file.
So you must to handling it with check if there is no path from dialog box.
If strfileopen <> "" Then 'so this line will execute if path is available
IntDemoFileNbr = FreeFile
Open StrFileToOpen For Binary Access Read As #IntDemoFileNbr
StrBuffer = Input(LOF(IntDemoFileNbr), IntDemoFileNbr)
TxtTestFile.Text = StrBuffer
Close #IntDemoFileNbr
mstrLastDir = Left$(StrFileToOpen, InStrRev(StrFileToOpen, "\") - 1)
End If
also the use of On Error Go To statement is a bad thing.
.CancelError = True
In line 4, Set CancelError to False.
.CancelError = False
Add this lines :
FirstNameTF.AutoCompleteSource = AutoCompleteSource.CustomSource
LastNameTF.AutoCompleteSource = AutoCompleteSource.CustomSource
In the properties of the MdiParent look for IsMdiContainer and set it to true.
@ Lee
Hey man thanks alot for responding. I have decided to go another route on this project and am not sure of I can close this thread. But... I relized after I posted that I didnt need a 2D array. I am now takeing a very different route.
@administrators
you can close this thread. It was a poorly attempted question and only supports my claim at being new to programming.
Thanks for the help
Just mark this thread as solved.
You're welcome.
This should working :
Private Sub Command1_Click()
For Each ctl In Me.Controls
If TypeOf ctl Is CheckBox Then
If ctl.Value = vbChecked Then
MsgBox ctl.Name
End If
End If
Next
End Sub
This is how to find the real roots using Newton’s method:
Function realCubeRoot(a, b, c, d, n)
'
' computes the nth real root of the cubic equation
'
' a x^3 + b x^2 + c x + d = 0
'
' =================================================
xold = 1
iter = 0
Do
iter = iter + 1
f = a * xold ^ 3 + b * xold ^ 2 + c * xold + d
df = 3 * a * xold ^ 2 + 2 * b * xold + c
xnew = xold - f / df
Err = xnew - xold
xold = xnew
Loop While (iter < 1000) And (Abs(Err) > 0.000001)
If n = 1 Then
realCubeRoot = xnew
Else
aa = b / a
bb = c / a
Real = -(aa + xnew) / 2
Disc = (-3 * xnew ^ 2 - 2 * aa * xnew + aa ^ 2 - 4 * bb)
If Disc < -0.0000001 Then
realCubeRoot = "NA"
Else
Disc = Abs(Disc)
If n = 2 Then
realCubeRoot = Real + Disc ^ (1 / 2) / 2
Else
realCubeRoot = Real - Disc ^ (1 / 2) / 2
End If
End If
End If
End Function
Private Sub Command1_Click()
MsgBox realCubeRoot(5, 2, 2, 4, 1)
End Sub
I want it in 4 columns (First name, last name, age and blood type).
There are 4 arrays that i want it to written in list view but the point is how i can read it form text file and use listview to show it.
This should working :
Private Sub Command1_Click()
Dim temp() As String
temp = Split(ReadFileText("D:\Mid Task\Data.txt"), vbNewLine)
With ListView1
.View = lvwReport
.FullRowSelect = True
.ColumnHeaders.Add , , "First Name", 1100
.ColumnHeaders.Add , , "Last Name", 1400
.ColumnHeaders.Add , , "Age", 700
.ColumnHeaders.Add , , "Blood Type", 1000
With .ListItems
.Clear
For i = 0 To UBound(temp)
temp2 = Split(temp(i), ";")
Set lsvItem = .Add(, , temp2(0))
lsvItem.SubItems(1) = temp2(1)
lsvItem.SubItems(2) = temp2(2)
lsvItem.SubItems(3) = temp2(3)
Next i
End With
End With
I think you don't have to use another arrays. Read data to temp array and write it to listview.
Hope it helps.
Try this :
Private Sub Form_Load()
Dim Conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Set Conn = New ADODB.Connection
Conn.Provider = "Microsoft.ACE.OLEDB.12.0"
Conn.CursorLocation = adUseClient
Conn.Open "D:\database.accdb"
Set rs = New ADODB.Recordset
rs.Open "select * from mytable", Conn, adOpenDynamic, adLockBatchOptimistic
While Not rs.EOF
Combo1.AddItem rs!name
rs.MoveNext
Wend
rs.Close
End Sub
Not really understand what you want exactly.
This is code already test and working fine.
I'm using keypress event just for textbox not for the button.
If you want to use button to play it then use button default event (click event) and don't change it with any events like keypress event.
Also please confirm that your textbox name is URLAddr and button name is Command1 to make this working.
I hope you can handle it.
Private Sub Command1_Click()
Dim VideoAddr As String
VideoAddr = Replace(URLAddr.Text, "/watch?v=", "/v/")
WindowsMediaPlayer1.URL = VideoAddr
End Sub
I already build a program with vb6. The problem is when the program is running and user click the exe file again then it makes program running twice in same time.
Is there a way to restrict this happened?
Something like this should working :
Private Sub Form_Load()
If App.PrevInstance = True Then
MsgBox "Application is running!", vbExclamation, "Warning"
End
End If
End Sub
Is there away to do this in VB6
Yes. there's a way.
You must have Windows Media Player instaled in your computer.
First, add new component ( Windows Media Player)
You can press ctrl+T or Project->Add Component, then select Windows Media Player and press OK.
New component will added to your toolbox. Draw it to your form and also add Textbox to your form.
I'm Using Keypress event in textbox. After user input url in textbox and press enter then it will connecting.
Private Sub URLAddr_KeyPress(KeyAscii As Integer)
Dim VideoAddr As String
VideoAddr = Replace(URLAddr.Text, "/watch?v=", "/v/")
If KeyAscii = 13 Then
WindowsMediaPlayer1.URL = VideoAddr
End If
End Sub
This the snapshot :
Try :
Private Sub Command2_Click()
Kill (App.Path & "\backup\PhoneTel.mdb")
End Sub
Or you can do like this :
Private Sub filedelete(filename As String)
Dim filesystemobject As Object
Set filesystemobject = CreateObject("Scripting.filesystemobject")
filesystemobject.deletefile filename, True
End Sub
Private Sub Command2_Click()
filedelete (App.Path & "\backup\PhoneTel.mdb")
End Sub
dIndex = myText.IndexOf("|")
...
So, is there a "length" command to find the total number of characters in the string?
To get total character/length :
sLength = myText.Length
But if you want to get string after | sign then you can use substring function :
PrinterId = myText.Substring(myText.IndexOf("|") + 1)