How to know that the navigate function didnt work [VBA] - forms

How do I prove that navigate function worked? I have this code:
Dim oBrowser As InternetExplorer
Set oBrowser = New InternetExplorer
oBrowser.Visible = True 'oBrowser.Silent = True
oBrowser.navigate "www.google.com"
I tried looking here but I don't get it: the return value is a long type? I tried:
Dim test as long
test = oBrowser.navigate "www.google.com"
msgbox test
'to see the value of the return
But it wont work

To add to the other answer: You could just use the below code, and not worry at all about the return of navigate. You could use MsgBox (as I have below to match your question), you could use a Boolean Success = True / False where relative, or just an old Debug.Print("Success/Error") where needed.
Then add the error handling into that.
Sub ie_open()
On Error Goto MyErrorCode
Dim ie As Object
Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
ie.NAVIGATE "http://www.google.com"
ie.Visible = True
MsgBox "Hurray this worked!"
MyErrorCode:
MsgBox "An error Occured!" & Err
End Sub

If I understood you properly you want to know whether the code snippet works or not? Right? If that's the case you simply do some error handling and if there's an error then you will know that there's some error. An example is below
If IsError(write statement you want to test) Then
goto errorMessage
Else
'do something if there is a match
End If
errorMessage:
'The handling of the error

Related

How can I save the value of an ID from IE to a variable in VBScript?

I would like to write a program that outputs the email from the website "https://10minutemail.net/" in a message box. Unfortunately, I can't find any code that allows me to store the ID in a variable.
I have tried this already without any results:
Dim email
email = ""
set webbrowser = createobject("internetexplorer.application")
webbrowser.statusbar = false
webbrowser.menubar = false
webbrowser.toolbar = false
webbrowser.visible = false
webbrowser.navigate("10minutemail.net")
email = webbrowser.document.all.item("fe_text")
wscript.echo(email)
The code runs with the Error: [unknown error][1].
[1]: https://i.stack.imgur.com/m1yHF.png
How to fix that?
Vbscript use HTML DOM with this method getElementById to get the particular element in a HTML code source page
So in your case, you can give a try with this code :
Option Explicit
Dim IE,Email_Value
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.navigate "https://10minutemail.net/"
Do While (IE.Busy)
WScript.Sleep 100
Loop
Email_Value = IE.document.getElementByID("fe_text").Value
wscript.echo(Email_Value)
IE.Quit()

Ms Access - Button with code doesn't work to send email

I have this code set in access, but no email is sending upon clicking the button on the form. I have outlook open. When i click the button on the form, i can't see anything that actually happens. I want the email address to be equal to the value in [text1], and I am trying to make the subject include a fixed message plus the input from [text2]. Even without these variables, I can't get this to work
Public Sub Command495_Click()
Dim mailto As String
Dim ccto As String
Dim bccto As String
mailto = [text1]
ccto = ""
bccto = ""
emailmsg = "trial"
mailsub = [text2] & ", Does this work?"
On Error Resume Next
DoCmd.SendObject acSendNoObjectType, , acFormattxt, mailto, ccto, bccto, mailsubj, emailmsg, True
End Sub
I have checked to make sure the onclick property shows event procedure. I am stuck, please help!
Here are a few suggestions and a modified version of your code.
ALWAYS use Option Explicit and compile your module before testing. You had a number of variables that were not defined and incorrect spelling of some options.
NEVER bypass errors when testing (get rid of your "On Error Resume Next") That's why you never saw an error.
Look for every place I entered ">>>" and address that issue.
Always explicitly define your variables and use the proper Type. Removes all doubt of what/where something is.
Option Compare Database
Option Explicit
Public Sub Command495_Click()
Dim mailto As String
Dim ccto As String
Dim bccto As String
Dim emailmsg As String
Dim mailsub As String
mailto = [Text1] ' >>> Where is [Text1]?? Remove for testing
ccto = ""
bccto = ""
emailmsg = "trial"
mailsub = [Text2] & ", Does this work?" ' >>> Where is [Text2]?? Remove for testing
' >>> Bad idea to ignore errors when testing!!
On Error Resume Next
'>>> Following line had: (1) 'acSendNoObjectType' which is incorrect; (2) mailsubj, which is undefined
DoCmd.SendObject acSendNoObject, , acFormatTXT, mailto, ccto, bccto, mailsub, emailmsg, True
End Sub

Can't change Visible property in Label of a VBA UserForm

I've got a Excel form named UserForm1 with a label with this properties:
This label has the property Visible=False. I want to make visible the label when the user click the CommandButton1, for that I wrote the next code:
Private Sub CommandButton1_Click()
Me.Label6.Visible = True
Dim oficina_garsa, file_source, file_solds As String
Dim invoice_year, invoice_month As Integer
oficina_garsa = TextBox3.Value
file_source = TextBox1.Value
file_solds = TextBox2.Value
invoice_year = CInt(ComboBox1.Value)
invoice_month = ComboBox2.ListIndex
Debug.Print oficina_garsa, file_source, file_solds, invoice_year, invoice_month
Call MainProcess(oficina_garsa, file_source, file_solds, invoice_year, invoice_month)
End Sub
But it doesn't work. So I wrote other event code linked to CommandButton2 to testing purpose like this:
Private Sub CommandButton2_Click()
If Me.Label6.Visible = False Then
Me.Label6.Visible = True
Else
Me.Label6.Visible = False
End If
Application.Wait Now + TimeValue("00:00:03")
End Sub
And the last code works fine while the first one doesn't! But in the first code the next code after Me.Label6.Visible = True runs whole without error message.
I've tried replacing 'Me' by 'UserForm1', but the result is the same. Why assigning the Visible property to True works in the CommandButton2 event but doesn't in the CommandButton1 one?
I know this thread is quite old, but as I came across it with the same problem, I thought I'd post the fix.
After setting the visible argument, add a line
Me.Repaint
This will re-render the form and update the visibility of the element. I think the Repaint command is specific to User Forms, so not sure if this will work in other situations where elements are not rendering accurately.

Access VBA visible control form from another form

I have a table and form setup to control another form in my database.
I'm wanting to make a code that will take the title from my field and add it to my code as a variable to change the visibility options of my other form.
my form is set with all the of the names to all objects on the form I want to control.
LSE_FORM_ADMIN = The table with all the LSE_FORM_ALL names in it.
Table is setup with 3 columns key, names and a checkbox which I put into a form to make a continuous list.
here is my code on the form, but I keep getting and runtime 424: object required error:
Private Sub Form_Current()
Dim VARSET As Object
Dim VAR As String
VARSET = DLookup("TITLE", Table!LSE_FORM_ADMIN, "") 'keep getting error here
VAR = VARSET
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!VAR.Visible = True
Else
Form_LSE_FORM_ALL!VAR.Visible = False
End If
End Sub
can someone help me fix this code so that it will grab the title field data and make it a variable to add to the rest of the code?
It's difficult to see exactly what you are trying to achieve, but your problems stem from using the variant variable type when you should be using an explicit Form or Control type. Using your last example.
RSTT.Visible = True 'getting Run-time error '424': object required
This is because you have declared RSTT as a variant. The line
RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
results in the variable RSTT containing a string, which does not have a property ".Visible"
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
These lines are redundant as you have the values that you need available on the form fields which are already bound to the table LSE_FORM_ADMIN.
As far as I understand, you have a continuous form (ADMIN?) bound to the table LSE_FORM_Admin. As you step through the records on this form, you want code to be fired which takes the value of the TITLE field/control and use it to set a control with the same name, on a separate form, Form_LSE_FORM_ALL, to be (in)visible, dependent on the value of the checkbox control name CB on the ADMIN form?
If you want the ADMIN form to make the changes "live" to the ALL form, you should consider using an event of the CB checkbox control. Using the current event of the form means that the changes you make will not be reflected in the ALL form until you step out of the record you have just edited, then back in, to fire the form's Current event on that record.
Example using AfterUpdate event of CB checkbox
Private Sub CB_AfterUpdate()
Dim strRST As String
Dim frmTarget as Form
Dim ctlRSTT As Control
Set strRST = Me!TITLE
Set frmTarget = Forms("Form_LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
ctlRSTT.Visible = Me!CB 'getting Run-time error '424': object required
End Sub
really not sure how to do the syntax when doing a recordset to a table from the form, need some help with that.
here is my code and attempt at the record set:
Private Sub Form_Current()
Dim DB As Database
Dim RS As Recordset
Dim RST As String
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Set RST = RS 'GETTING OBJECT REQUIRED ERROR ON "RST ="
Do Until RS.EOF
RST = Me!TITLE
RS.MoveNext
Loop
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!RS.Visible = True
Else
Form_LSE_FORM_ALL!RS.Visible = False
End If
End Sub
I think I know what you are trying to do, but your descriptions / references are not matching up. Please look at the following comments and clarify:
1. You say "...make a code that will take the title from my field and ..." but your code is taking "Me.Title", "ME" is a reference to the Form - not a field.
2. Your code is in the "Form_Current" event, which means it will fire for every record you process. That will work, but I think you want to do this code only once to be more efficient.
3. You have no provision for processing more than one field. I think you need to loop through all fields in your table, setting visible to true or false.
The following is my suggestion, but I will update once you clarify the issues.
Option Compare Database
Option Explicit
Dim DB As DAO.Database
Dim RS As DAO.Recordset
'Dim RST As Variant
'Dim RSTT As Variant
Public Sub FORM_CURRENT()
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Do While Not RS.EOF ' Loop thru all field names for the form
If RS!HideYN = True Then ' Desire to hide the field?
Me(RS!ctlname).Visible = False ' Yes, hide the field.
Else
Me(RS!ctlname).Visible = True ' No, show the field
End If
RS.MoveNext ' Get next field name
Loop
RS.Close
Set RS = Nothing
Set DB = Nothing
'Set RST = Me!Title
'RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
'If Me!CB = "-1" Then
' RSTT.Visible = True 'getting Run-time error '424': object required
'Else
' RSTT.Visible = False
'End If
End Sub
Final code, thanks to Cheesenbranston.
Private Sub Form_AfterUpdate()
Dim strRST As String
Dim frmTarget As Form
Dim ctlRSTT As Control
strRST = Me!TITLE
Set frmTarget = Forms("LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
If Me!CB = "-1" Then
ctlRSTT.Visible = True
Else
ctlRSTT.Visible = False
End If
End Sub
#Cheesenbranston: Your original code was more like a toggle of on and off so if my object was not visible then my trigger checkbox would make it visible when checked, more of a quality of life for my own needs, none the less worked. Also strRST doesn't need SET since its just a String. Thanks again =D very happy day!

Error Saving the Excel File using worksheet.saveas

I have an Excel template file . Based on the Excel version, I would like to SaveAs as Temp.xlsm through Matlab.
Here is the code I am using to save the template file :
if(XLversion >= 12.0)
Workbook = invoke(Excel.Workbooks,'Open',tempxls);
tempxls1 = [pwd '\utils\temp.xlsm'];
Workbook.SaveAs(tempxls1,1);
tempxls = tempxls1;
extn = 'xlsm';
end
The code is working fine. However when I try to open the file, I get the following error :
However, when the save the temp.xls to temp.xlsm through SaveAs menu, it opens without any error.
Any idea what could be the error in the code or If I am missing something.
Thanks
Can you try the following code please and comment. You may call this sub-routine to save the as the file.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim FileNameVal As String
If SaveAsUI Then
FileNameVal = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
Cancel = True
If FileNameVal = "False" Then 'User pressed cancel
Exit Sub
End If
Application.EnableEvents = False
If Right(ThisWorkbook.Name, 5) <> ".xlsm" Then
ThisWorkbook.SaveAs Filename:=FileNameVal & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
ThisWorkbook.SaveAs Filename:=FileNameVal, FileFormat:=xlOpenXMLWorkbookMacroEnabled
End If
Application.EnableEvents = True
End If
End Sub
Reference: