I have a problem trying to catch the completion of a stored proc execute asynchronously.
Below my code VBA (in a class module named clsAsync):
Option Explicit
Private WithEvents cnn As ADODB.Connection
Private Sub cnn_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
MsgBox "Execution completed"
End Sub
Sub execSPAsync()
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
cnn.ConnectionString = "connection to my database SQLSEREVER"
cnn.Open
cnn.Execute "kp.sp_WaitFor", adExecuteNoRecords, adAsyncExecute
End Sub
This class is PublicNotCreatable.
To call the sub execSPAsync from a module I use the following code:
Sub testASYNC()
Dim a As New clsAsync
Call a.execSPAsync
End Sub
The stored procedure is very simple:
alter PROC kp.sp_WaitFor
AS
WAITFOR DELAY '00:00:05'
My problem is that the event ExecuteComplete is not fired at all, while if I comment the adAsynExecute parameter all is working fine.
Any idea on how to solve my question?
I solved my problem replacing the calling code:
Sub testASYNC()
Dim a As New clsAsync
Call a.execSPAsync
End Sub
with this new code:
Private a As clsAsync
Sub testASYNC()
Set a = New clsAsync
Call a.execSPAsync
End Sub
In the async mode, the object "a" is no longer available at the end of the procedure (scope visibility issue).
Related
How do I Get the Login Cookies when am logged into a site?
I am searching for answers and now turned to StackOverflow pro members for your expertise and help to get this out of the way so I can move on to making my software.
i need to get the cookies login/visit /get/post anything how do i get it using the new webview2 browser kindly shed some light please anyone
Option Explicit
'Note, that this Demo requires the properly registered RC6-Binaries
'and in addition an installed "Chromium-Edge" (in its "evergreen" WebView2-incarnation)
'installable from its official MS-Download-URL: https://go.microsoft.com/fwlink/p/?LinkId=2124703
Private WithEvents WV As cWebView2 'declare a WebView-variable WithEvents
Private Sub Form_Load()
Visible = True '<- it's important, that the hosting TopLevel-Form is visible...
'...(and thus the Child-PicBox indirectly as well) - before we Bind the PicBox to the WebView
Set WV = New_c.WebView2 'create the instance
If WV.BindTo(picWV.hWnd) = 0 Then MsgBox "couldn't initialize WebView-Binding": Exit Sub
' Set WV = New_c.WebView2(picWV.hWnd) 'create the instance
' If WV Is Nothing Then MsgBox "couldn't initialize WebView-Binding": Exit Sub
End Sub
'*** VB-Command-Button-Handlers
Private Sub cmdNavigate_Click()
WV.Navigate "https://google.com" '<- alternatively WV.jsProp("location.href") = "https://google.com" would also work
'the call below, just to show that our initially added js-functions, remain "in place" - even when we re-navigate to something else
WV.jsRunAsync "test", 2, 3
End Sub
Private Sub WV_NavigationCompleted(ByVal IsSuccess As Boolean, ByVal WebErrorStatus As Long)
Debug.Print "NavigationCompleted welaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
End Sub
Private Sub picWV_Resize() 'when the hosting picBox got resized, we have to call a syncSize-method on the WebView
If Not WV Is Nothing Then WV.SyncSizeToHostWindow
End Sub
Private Sub picWV_GotFocus() 'same thing here... when the hosting picBox got the focus, we tell the WebView about it
If Not WV Is Nothing Then WV.SetFocus
End Sub
'*** the above two EventHandlers (of the hosting VB-PicBox-Container-Ctl) are basically all what's needed "GUI-Binding-wise"
'*** the rest of the EventHandlers below, are raised by the WebView-instance itself
Private Sub WV_InitComplete()
Debug.Print "WV_InitComplete"
End Sub
'Private Sub WV_NavigationCompleted(ByVal IsSuccess As Long, ByVal WebErrorStatus As Long)
'Debug.Print "WV_NavigationCompleted"
'End Sub
Private Sub WV_DocumentComplete()
Debug.Print "WV_DocumentComplete"
End Sub
Private Sub WV_GotFocus(ByVal Reason As eWebView2FocusReason)
Debug.Print "WV_GotFocus", Reason
End Sub
Private Sub WV_JSAsyncResult(Result As Variant, ByVal Token As Currency, ByVal ErrString As String)
Debug.Print "WV_JSAsyncResult "; Result, Token, ErrString
Text2.Text = Result
End Sub
Private Sub WV_JSMessage(ByVal sMsg As String, ByVal sMsgContent As String, oJSONContent As cCollection)
Debug.Print sMsg, sMsgContent
Select Case sMsg
Case "btn1_click": MsgBox "txt1.value: " & WV.jsProp("document.getElementById('txt1').value")
End Select
End Sub
Private Sub WV_LostFocus(ByVal Reason As eWebView2FocusReason)
Debug.Print "WV_LostFocus", Reason
End Sub
Private Sub WV_UserContextMenu(ByVal ScreenX As Long, ByVal SreenY As Long)
Debug.Print "WV_UserContextMenu", ScreenX, SreenY
End Sub
Why am I getting an error "Sub or Function is not defined"...Here is my code
FORM2
Option Explicit
Public Report As New CrystalReport1
Public mvCn As New ADODB.Connection
Public Function printReport()
Dim strConnectionString As String
Dim rs As ADODB.Recordset
Dim strScript As String
strConnectionString = "Provider=SQLOLEDB............"
mvCn.ConnectionString = strConnectionString
mvCn.CommandTimeout = 0
mvCn.CursorLocation = adUseClient
mvCn.Open
strScript = strScript & "SELECT * FROM employee" & vbCrLf
Set rs = mvCn.Execute(strScript)
Report.Database.SetDataSource rs
Report.AutoSetUnboundFieldSource crBMTNameAndValue
CRViewer1.ReportSource = Report
CRViewer1.ViewReport
Set Report = Nothing
End Function
Form 1.....Call my function "printReport" here
Option Explicit
Private Sub Command1_Click()
printReport
End Sub
The error message goes here "Private Sub Command1_Click()"
Where is your printReport function defined? If it's in a class module, then you need to instantiate an instance of the class then call printReport as a method of that class. For instance:
Private Sub Command1_Click()
Dim oClass As New Class1
oClass.printReport
End Sub
Or you can place your printReport function in a module, then you don't instantiate it or call it as a method - you would instead call it as you have in your click event.
A procedure can be called in such a simple way.[As you have called is correct]
Eg.
Private Sub Form_Load()
Test1
End Sub
Sub Test1()
MsgBox "Test1"
End Sub
I've a simple winsock server chat and this is the code:
Private Sub Form_Load()
Winsock1.LocalPort = 5100
Winsock1.Listen
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim sData As String
Winsock1.GetData sData
Text1.Text = Text1.Text & sData
End Sub
I receive the message from an ios app but I can't send message with a "sendData".
How can I send message to all the clients? I need to use a client?
Thanks.
If you want to send messages to more than one client, then the best approach would be instead of closing your listening winsock1, and using it to accept the request, to create a new winsock control that will accept the request. This way you can accept connections from more than one source.
Example:
1st change winsock1's property Index to 0, to create a control array. Now all events's signature change to include the Index parameter.
Dim NumSockets As Integer
Private Sub Form_Load()
Winsock1(0).LocalPort = 5100
Winsock1(0).Listen
End Sub
Private Sub Winsock1_Close(Index As Integer)
Winsock1(Index).Close
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
NumSockets = NumSockets + 1
Load Winsock1(NumSockets) 'create a new winsock control
Winsock1(NumSockets).Accept requestID 'use that one to accept the request
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim vtData As String
Winsock1(Index).GetData vtData, vbString
Print vtData
End Sub
I was following this MSDN guide on creating custom events. I feel like I understand the process now, but I cannot figure out why I am getting a Compile Error: Event Not Found for RaiseEvent ItemAdded. The weird thing is, the ItemAdded event is recognized by the IDE (I can type it in all lowercase and it is then automatically formatted properly), so I know that it is recognized by VB.
DataComboBox Class Module Code:
Public Event ItemAdded(sItem As String, fCancel As Boolean)
Private pComboBox As Control
Public Property Set oComboBox(cControl As Control)
Set pComboBox = cControl
End Property
Public Property Get oComboBox() As Control
oComboBox = pComboBox
End Property
Private Sub Class_Initialize()
End Sub
Private Sub Class_Terminate()
End Sub
The UserForm contains two controls - a CommandButton named btnAdd and a ComboBox named cboData.
UserForm Code:
Private WithEvents mdcbCombo As DataComboBox
Private Sub UserForm_Initialize()
Set mdcbCombo = New DataComboBox
Set mdcbCombo.oComboBox = Me.cboData
End Sub
Private Sub mdcbCombo_ItemAdded(sItem As String, fCancel As Boolean)
Dim iItem As Long
If LenB(sItem) = 0 Then
fCancel = True
Exit Sub
End If
For iItem = 1 To Me.cboData.ListCount
If Me.cboData.List(iItem) = sItem Then
fCancel = True
Exit Sub
End If
Next iItem
End Sub
Private Sub btnAdd_Click()
Dim sItem As String
sItem = Me.cboData.Text
AddDataItem sItem
End Sub
Private Sub AddDataItem(sItem As String)
Dim fCancel As Boolean
fCancel = False
RaiseEvent ItemAdded(sItem, fCancel)
If Not fCancel Then Me.cboData.AddItem (sItem)
End Sub
You cannot raise an event outside the classes file level.
Add a routine like this inside "DataComboBox1" to allow you to raise the event externally.
Public Sub OnItemAdded(sItem As String, fCancel As Boolean)
RaiseEvent ItemAdded(sItem, fCancel)
End Sub
Then call the OnItemAdded with the current object.
Example...
Private WithEvents mdcbCombo As DataComboBox
...
mdcbCombo.OnItemAdded(sItem, fCancel)
I am trying to write 2 simple subroutines in VB 2010 that could be used with toolstripbutton contorls for multiple textboxes in a form. I know simple copy paste could be done using the textbox1.Copy() and TextBox1.Paste() methodes. What i am trying to do is write a common subroutine which could be used on any textboxes in the form not just one particular textbox.
My codes are below, I know there are errors in it, just wondering how it could be achieved. Any help would be highly appreciated. Thanks.
Public Class Form1
Private Sub copytext()
Dim txt As Control
If TypeOf txt Is TextBox Then
Clipboard.Clear()
Clipboard.SetText(txt.SelectedText)
End If
End Sub
Private Sub pastetext()
Dim txt As Control
If TypeOf txt Is TextBox Then
txt.Text = Clipboard.GetText
End If
End Sub
Private Sub mnuCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCopy.Click
Call copytext()
End Sub
Private Sub mnuPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPaste.Click
Call pastetext()
End Sub
End Class