|
That's the very wrong way to do it. It's lazy and leads to other possible issues that can't be traced down easily.
If there is code that needs to be executed from multiple places you stick it in its own method with a name that has meaning as to what the code does. You don't "hide" it inside an event handler.
|
|
|
|
|
its up to the developer to decide... and ofcourse the one who asked this question knows how to move all the statements in the button's click event to a sub-routine and call the same sub-routine in the button's click event. its that simple..let them do in their own way. can you describe why it is wrong way?
|
|
|
|
|
|
<br />
Option Explicit<br />
Dim DB As ADODB.connection<br />
Dim strString As String<br />
Dim rs As ADODB.recordset<br />
Dim cs As ADODB.connection<br />
Dim ks As ADODB.recordset<br />
Private Const CB_SHOWDROPDOWN As Long = &H14F<br />
Private Const CB_GETDROPPEDSTATE As Long = &H157<br />
Private Drop As Boolean<br />
Private Const CB_FINDSTRING = &H14C<br />
Private Const CB_SELECTSTRING = &H14D<br />
Private Const CB_LIMITTEXT = &H141<br />
Private Const CB_ERR = (-1)<br />
Private Sub db_conn()<br />
Set DB = New ADODB.connection<br />
<br />
DB.CursorLocation = adUseClient<br />
<br />
DB.Open "PROVIDER = Microsoft.Jet.OLEDB.4.0;Data Source =" & App.Path & "\Medrar.mdb;"<br />
End Sub<br />
Private Sub cmdAcchu_Click()<br />
Dim obj, rs As ADODB.recordset, SQL As String<br />
<br />
Set rs = New ADODB.recordset<br />
<br />
<br />
Set rs = New ADODB.recordset<br />
strString = " SELECT cusname,idnumb,mobi,pron,proc,blkno,lannum,sqland,cost,comiso,addit,deduc,gtot,vali,rename" & _<br />
" From reser" & _<br />
" WHERE reno = '" & Combo1.Text & "' "<br />
<br />
<br />
rs.Open strString, DB, adOpenStatic<br />
' Set rs = db.Execute(strString)<br />
'With reportPS<br />
If Not rs.EOF Then<br />
Set Reservation.DataSource = rs.DataSource<br />
<br />
<br />
'-----------------------------------------------------------------------------------------------'<br />
'PageHeader<br />
Reservation.Sections("PageHeader").Controls("lblcusname").Caption = CStr(rs!cusname)<br />
Reservation.Sections("PageHeader").Controls("lblidnumber").Caption = CStr(rs!idnumb)<br />
Reservation.Sections("PageHeader").Controls("lblmobile").Caption = CStr(rs!mobi)<br />
Reservation.Sections("PageHeader").Controls("lblPron").Caption = CStr(rs!pron)<br />
Reservation.Sections("PageHeader").Controls("lblProc").Caption = CStr(rs!proc)<br />
Reservation.Sections("PageHeader").Controls("lblBlkno").Caption = CStr(rs!blkno)<br />
Reservation.Sections("PageHeader").Controls("lblLn").Caption = CStr(rs!lannum)<br />
Reservation.Sections("PageHeader").Controls("lblsql").Caption = CStr(rs!sqland)<br />
Reservation.Sections("PageHeader").Controls("lblcost").Caption = CStr(rs!cost)<br />
Reservation.Sections("PageHeader").Controls("lbCommi").Caption = CStr(rs!comiso)<br />
Reservation.Sections("PageHeader").Controls("lblAdd").Caption = CStr(rs!Addit)<br />
Reservation.Sections("PageHeader").Controls("lblDed").Caption = CStr(rs!deduc)<br />
Reservation.Sections("PageHeader").Controls("lblGt").Caption = CStr(rs!Gtot)<br />
Reservation.Sections("PageHeader").Controls("lblVali").Caption = CStr(rs!vali)<br />
Reservation.Sections("PageHeader").Controls("lblrname").Caption = CStr(rs!rename)<br />
Reservation.Sections("PageHeader").Controls("lblcname").Caption = CStr(rs!cusname)<br />
Reservation.Sections("PageHeader").Controls("lblvali1").Caption = CStr(rs!vali)<br />
'-----------------------------------------------------------------------------------------------'<br />
<br />
<br />
Reservation.Refresh<br />
Reservation.Show vbModal<br />
<br />
<br />
End If<br />
'End With<br />
<br />
<br />
'For Each obj In reportPS.Sections("PageHeaders").Controls<br />
' If TypeOf obj Is RptTextBox Then<br />
' obj.DataMember = rs.DataMember<br />
<br />
'End If<br />
'Next<br />
<br />
<br />
End Sub<br />
Private Sub cmdbye_Click()<br />
Unload FrmResPrnt<br />
End Sub<br />
Private Sub Combo1_Click()<br />
Dim cnemployee As New ADODB.connection<br />
Dim rsemployee As New ADODB.recordset<br />
<br />
Call connection(cnemployee, App.Path & "\Medrar.mdb", "endromida")<br />
Call recordset(rsemployee, cnemployee, "SELECT * FROM Reser WHERE Reno ='" & Combo1.Text & "'")<br />
<br />
If rsemployee.RecordCount = 0 Then<br />
MsgBox "The record you requested could not be found.", vbExclamation, "Medrar"<br />
Exit Sub<br />
End If<br />
<br />
With rsemployee<br />
<br />
<br />
End With<br />
<br />
Set cnemployee = Nothing<br />
Set rsemployee = Nothing<br />
End Sub<br />
Private Sub Combo1_KeyPress(KeyAscii As Integer)<br />
Dim t$, rez&, l&<br />
Combo1.SelText = ""<br />
t$ = Combo1.Text & Chr$(KeyAscii)<br />
rez& = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal t$)<br />
If rez& <> CB_ERR Then<br />
l& = Len(t$)<br />
Combo1.Text = Combo1.list(rez&)<br />
Combo1.ListIndex = rez&<br />
KeyAscii = 0<br />
Combo1.SelStart = l&<br />
Combo1.SelLength = Len(Combo1.Text) - l&<br />
End If<br />
Dropdown<br />
End Sub<br />
Private Sub Combo1_LostFocus()<br />
Dim t$, rez&<br />
t$ = Combo1.Text<br />
rez& = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal t$)<br />
End Sub<br />
Private Sub Dropdown()<br />
If Drop Then<br />
If SendMessage(Combo1.hwnd, CB_GETDROPPEDSTATE, 0, ByVal 0&) = 0 Then<br />
Call SendMessage(Combo1.hwnd, CB_SHOWDROPDOWN, 1&, 0&)<br />
End If<br />
End If<br />
End Sub<br />
Private Sub res()<br />
Set cs = CreateObject("Adodb.connection")<br />
cs.Provider = "Microsoft.jet.oledb.4.0"<br />
cs.Open App.Path & "\Medrar.mdb"<br />
Set ks = New ADODB.recordset<br />
ks.Open "Select reno from reser", cs, adOpenKeyset, adLockOptimistic<br />
While Not ks.EOF<br />
Combo1.AddItem ks.Fields("Reno")<br />
ks.MoveNext<br />
Wend<br />
ks.Close<br />
Set ks = Nothing<br />
Drop = True<br />
End Sub<br />
Private Sub Form_Load()<br />
Call res<br />
End Sub<br />
<br />
Run Time Error:3709
|
|
|
|
|
please read my sig.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
Maybe this[^] will help you... or even us.
|
|
|
|
|
First, you'll have to find out what's causing the error; Knowledge Base[^]
Next, you fix it
Bastard Programmer from Hell
|
|
|
|
|
Are there no bounds to your wisdom at all?
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
MY GOD MAN! HOW DARE YOU SEARCH THE SACRED KNOWLEDGE BASE!
Stay away from me. I don't want to get hit by the lightning that's coming for you.
|
|
|
|
|
|
Hi,
I get the following message when I try and delete a file.
"System.IO.IOException - The process cannot access the file 'filename' because it is being used by another process."
My cut down code is as follows
Dim aryFi As IO.FileInfo() = diFolder.GetFiles("*.pdf")
For Each fi In aryFi
sFileName = fi.Name
fi.delete
Next
Basically what I am doing is that depending what file is in the folder, delete it once I get various information about it that file. The above code is a cut down version. What other way would you delete a file or is this the best way. If so, what am I doing wrong.
Thanks in advance.
modified on Wednesday, May 25, 2011 8:30 AM
|
|
|
|
|
The "other process" still has the file open. There's no way to delete it until the other process closes the file.
You could force it closed using some hacked techniques, but that'll only crash the other process that still has it open.
|
|
|
|
|
Hi,
that is one of the more confusing Windows/.NET error messages. The "other process" may well be your own process, it really should say "some process".
So it could be as well your own process having read or written the file, or having interrogated the file system about its characteristics. Make sure you don't have open streams, files, fileinfo's, etc; close and dispose them before executing the code that throws the exception.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
Maybe it should just say "... is currently in use."
Panic, Chaos, Destruction.
My work here is done.
or "Drink. Get drunk. Fall over." - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
|
|
|
|
|
Yes and no. Shortening the message is one way of improving the probability it hits the truth, but then it is also less informative. I'd rather have it say: "the file is currently in use, either by your own process or by some other process." That way, the reader knows he has to actually look for two quite different possibilities.
And then, of course, they should put in some more effort and find out which of those two it is, and provide the more specific message; either "the file is currently in use by your own process", or "the file is currently in use by another process (processname, processID)". But that would be helping the user, something Windows isn't inclined to do much.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
Hi,
I have a project created using vb.net 2005. I need to collect data and generate a input file for a EXE file which will take 2 min to execute the exe file.
I also create a form with progressbar and Stop button, since I cannot get status of the EXE file, I just set the value of the progressbar from 1 ~ 100 then repeat. codes as the following
Dim p As New System.Diagnostics.Process()
p.StartInfo.FileName = Application.StartupPath & "\" & sAppPath
p.StartInfo.Arguments = fileName
'Do not use the system shell to start the program this is so we can hide the command dos window
p.StartInfo.UseShellExecute = False
p.StartInfo.WorkingDirectory = strOutputFilePath
' Show no dos window if false
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardOutput = True
Dim progBarFrm As New FormProgressBar
progBarFrm.Show()
p.Start
''''''''''''''
But when I run the above code, the progressbar value doesn't change. But if I comment out p.Start line, the progressbar works fine.
How can I show the progressbar while the process is running, if you have some sample, that's best.
Thanks!
|
|
|
|
|
You can show the progress bar all you want. You don't haven't shown any code that updates the progress bar at all.
p.Start is not a blocking call, so your process will launch and run independent of this code.
|
|
|
|
|
On top of what Dave said, please use PRE tags when showing code snippets, that will improve readability.
And tell us where that code is sitting; and if threads are involved, we need to know that too.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Davd and Luc,
Thanks for your reply.
Acutally my code is like the following, I didn't use thread, I am not familiar with it.
Dim progBarFrm As New FormProgressBar
progBarFrm.Show()
try
p.Start()
Catch ex As Exception
ShowValidationMessage("Unexception error is caught, please close WinTAP and try again.", "Error")
progBarFrm.Close()
Return
End Try
rtnStr = p.StandardOutput.ReadToEnd()
p.WaitForExit(5400000)
If Not p.HasExited Then
p.Kill()
End If
progBarFrm.Close()
|
|
|
|
|
Hi,
Now I modify the code to as the following, but OnProcesited is never called, why?????
Dim progBarFrm as New FormProgressBar
Private Sub Run_Tool()
Dim sAppPath As String = ".\XXXX.exe"
Dim fileName As String = "xxxxxxxxxxxx"
Dim p As New System.Diagnostics.Process()
progBarFrm = New FormProgressBar
Try
p.Start()
p.EnableRaisingEvents = True
AddHandler p.Exited, AddressOf OnProcesited
progBarFrm.Show()
progBarFrm.Refresh()
Catch ex As Exception
ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
Return
End Try
''Shell(sAppPath, vbNormalFocus) 'other options for starting with
End Sub
Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
Dim p As System.Diagnostics.Process
p = sender
progBarFrm.Close()
progBarFrm.Dispose()
End Sub
|
|
|
|
|
You should prepare the process p completely before calling Start() on it; once launched, it is out of your control.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Thanks for reply,
I modify the code, but still don't work
|
|
|
|
|
Andraw Tang wrote: still don't work
That is not informative.
Anyway, you never made clear how the progress would be reported, as Dave asked. What is changing the ProgressBar's value? where is that code? when is it called? by whom? on what thread? etc.
Please explain the situation clearly, provide necessary information, then ask specific questions.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I thought I already give all the information needed.
The Run_Tool() will be called when user click "Run" item in the menu.
Inside Run_Tool(), a process will be created to run an exe file, at the same time, a form with progress bar will be created and displayed (let's forget about the progress bar value at this time), I also add an event Exited for the procee, when the event is called, close the form with progress bar, that's it.
Now the problem is that the Exited event is never called, so form progBarFrm is never closed.
Dim progBarFrm as FormProgressBar
Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
Dim p As System.Diagnostics.Process
p = sender
progBarFrm.Close()
progBarFrm.Dispose()
End Sub
Private Sub Run_Tool()
Dim sAppPath As String = ".\XXXX.exe"
Dim fileName As String = "DDDDDDDDDDDDDDDD.txt"
Dim p As New System.Diagnostics.Process()
p.StartInfo.FileName = Application.StartupPath & "\" & sAppPath
p.StartInfo.Arguments = fileName
'Do not use the system shell to start the program this is so we can hide the command dos window
p.StartInfo.UseShellExecute = False
p.StartInfo.WorkingDirectory = strOutputFilePath
' Show no dos window if false
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardOutput = True
p.EnableRaisingEvents = True
AddHandler p.Exited, AddressOf OnProcesited
progBarFrm = New FormProgressBar
Try
p.Start()
progBarFrm.Show()
progBarFrm.Refresh()
Catch ex As Exception
ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
'p.Kill()
Return
End Try
''Shell(sAppPath, vbNormalFocus) 'other options for starting with
End Sub
modified on Tuesday, May 24, 2011 2:21 PM
|
|
|
|
|
That clearly is not the entire story. Are you waiting for the process to exit somewhere? what is in the "Run clicked" handler? Is your app responsive (can you move its main window, will it repaint) while the process runs?
one handler can not execute while another one is still running!
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|