Option Explicit
Public Sub Main(): If Con.Initialize = conLaunchExplorer Then Con.Visible = False
Dim strParameters As String
Dim readL As String
strParameters = Split(Command$, "/")
Con.WriteLine strParamers
readL = Con.ReadLine
End Sub
This is code right now as you can see I'm trying to split the strParameters but the error says mismatch please help me.
When Splitting a string, the data is placed in an array, and according to your declaration strParameters is a simple string. How would the different parts fit in one string? You just need to change your declaration as follows;
Dim strParameters() As String
Assuming that your Command$ is correctly declared as string, with delimiters "/"
Related
I am trying to pass an array of fixed length strings to a function. However, the function will not know the size of the strings. The parameter should take any size strings. How would I go about this? Here is my code:
Public Type myRecord
names(15) As String * 250
End Type
Function def:
Function HasQuotes(textArr() As String) As String
End Function
Usage:
HasQuotes(rowRec.names)
where rowRec is a MyRecord.
When I run this code I get the following error Type Mismatch: array or user defined type expected. This is because the parameter is not defined as textArr() As String * 250. How can I make the parameter accept any string length?
You Have to follow Proper syntax you can Try Following updated Code
Private Type myRecord
names(15) As String * 250
End Type
Private Sub Command1_Click()
Dim rowRec As myRecord
rowRec.names(0) = ("Vaibhav")
Dim v As String
v = HasQuotes(rowRec.names)
End Sub
Function HasQuotes(ByRef textArr() As String * 250) As String
MsgBox textArr(0)
End Function
You are going to have trouble passing an array of fixed-length strings of any length. One simple solution is to convert them to an array of variable-length strings:
Private Sub Test()
Dim rowRec As myRecord
Dim names(15) As String
Dim i As Integer
Dim s As String
rowRec.names(0) = "Brian"
rowRec.names(15) = "Stafford"
'move the data to an array of variable-length strings
For i = 0 To 15
names(i) = rowRec.names(i)
Next
s = HasQuotes(names)
'move the data back to the original array if needed
For i = 0 To 15
rowRec.names(i) = names(i)
Next
End Sub
I have a word merge from vb.net application that works fine on other machines but on mine the source document suppress spaces in between column headers and errors out
expected header in source doc
sampledata1 sampledata2 samp3
created
sampledata1sampledata2samp3
Private mstrMailMergeFields As String = "CL_Number, Name, CO_NAME"
Dim strTemplateFileName As String
Dim strSourceFileName As String
Dim strSignatureFilePath As String = ""
Dim wordApp As Microsoft.Office.Interop.Word.Application = Nothing
Dim wordDoc As Microsoft.Office.Interop.Word.Document = Nothing
Dim wordDocSource As Microsoft.Office.Interop.Word.Document = Nothing
Dim wordDocResult As Microsoft.Office.Interop.Word.Document = Nothing
any direction on help is appreciated
Figured it out the regional settings in word has a field for separator was "!" instead of "," this was messing the header changed it and it worked fine.
I need help on how to remove spaces/emtpy in data without compromising spaces on other data. Here's my sample data.
12345," ","abcde fgh",2017-06-06,09:00,AM," ", US
expected output:
12345,,"abcde fgh",2017-06-06,09:00,AM,, US
since " " should be considered as null.
I tried the Trim() function but it did not work. I also tried Regex pattern but still no use.
Here's my sample function.
Private Sub Transform(delimiter As String)
Dim sFullPath As String
Dim strBuff As String
Dim re As RegExp
Dim matches As Object
Dim m As Variant
If delimiter <> "," Then
strBuff = Replace(strBuff, delimiter, ",")
Else
With re
.Pattern = "(?!\B""[^""]*)" & delimiter & "(?![^""]*""\B)"
.IgnoreCase = False
.Global = True
End With
Set matches = re.Execute(strBuff)
For Each m In matches
strBuff = re.Replace(strBuff, ",")
Next
Set re = Nothing
Set matches = Nothing
End If
End Sub
I think you're on the right track. Try using this for your regular expression. The two double quotes in a row are how a single double quote is included in a string literal. Some people prefer to use Chr(34) to include double quotes inside a string.
\B(\s)(?!(?:[^""]*""[^""]*"")*[^""]*$)
Using that expression on your example string
12345," ","abcde fgh",2017-06-06,09:00,AM," ", US
yields
12345,"","abcde fgh",2017-06-06,09:00,AM,"", US
Example function
Private Function Transform(ByVal strLine As String) As String
Dim objRegEx As RegExp
On Error GoTo ErrTransForm
Set objRegEx = New RegExp
With objRegEx
.Pattern = "\B(\s)(?!(?:[^""]*""[^""]*"")*[^""]*$)"
.IgnoreCase = False
.Global = True
Transform = .Replace(strLine, "")
End With
ExitTransForm:
If Not objRegEx Is Nothing Then
Set objRegEx = Nothing
End If
Exit Function
ErrTransForm:
'error handling code here
GoTo ExitTransForm
End Function
And credit where credit is due. I used this answer, Regex Replace Whitespaces between single quotes as the basis for the expression here.
I would add an output string variable and have a conditional statement saying if the input is not empty, add it on to the output string. For example (VB console app format with the user being prompted to enter many inputs):
Dim input As String
Dim output As String
Do
input = console.ReadLine()
If Not input = " " Then
output += input
End If
Loop Until (end condition)
Console.WriteLine(output)
You can throw any inputs you don't want into the conditional to remove them from the output.
Your CSV file isn't correctly formatted.
Double quote shouldn't exists, then open your CSV with Notepad and replace them with a null string.
After this, you now have a real CSV file that you can import whitout problems.
I have created and array which will store the value of the inputbox, it is saying i have a syntax error and i am unsure how to fix it.
I have used parameter passing which i will display below also
Dim name() As String
For counter = 1 To 5
Call enter_questionnaire_data(name()) '2.0
Next
End sub
2nd sub-routine
Private Sub enter_questionnaire_data(ByRef name())
name() = InputBox("Enter the party name")
why do you have name as a String array?
you just need to declare
Dim name As String
for allowing name to store a String
also you cannot assign values to array members like this
name() = InputBox("Enter the party name")
you need to specify the index also
EDIT:
if you want string array to store the names, then
declare a static array of sufficient length
Dim name(10) As String
and use:
name(index) = InputBox("Enter the party name")
index = index+1;
where the index is incremented after every input till 10
(using dynamic array would be a bit complicated for you right now , so i am omitting dynamic array from discussion)
Use name without brackets
Dim name As String
and in other method
Private Sub enter_questionnaire_data(ByRef name)
name = InputBox("Enter the party name")
About your program:
Dim name As String <---Without () you can use this for array
For counter = 1 To 5
Call enter_questionnaire_data(name as string)<--- can you insert variable/tipe
Next
End sub
Private Sub enter_questionnaire_data(name as string)
name = InputBox("Enter the party name")
Sorry to ask such a dumb question.. but for the life of me i cant get it.. i have searched EVERYWHERE... This is a Re-Creation of my code that gives the same error. This is the most basic example i could re-create.
I dont understand why i have to declare a Label ?? (or an object)
What I am trying to accomplish is use my main form to call all the modules.
This is the FORM
'frmMain.frm
Option Explicit
Public Sub btnOpen_Click()
GetNum
End Sub
This is the MODULE
'modGet.bas
Option Explicit
Public Sub GetNum()
Dim a As String
Dim b As String
a = "hello"
b = "world"
-> Label1.Caption = a 'ERROR, Compile Error, Variable not Defined. (vb6)
Label2.Caption = b
End Sub
YES, i have a form, with a Button named 'btnOpen', i have 2 Labels named 'Label1' & 'Label2'
If i ADD..
Dim Label1 As Object 'in MODULE
i get a different error..
ERROR '91' Object Variable or With block variable not set
IF I put everything in 1 FORM, it works..(but i want to use separate modules)
I Commented out 'OPTION EXPLICIT' ... same error.
In another Test, i got the error for a TextBox..
TextBox1.Text = x
Once i get the answer for this, i can apply it for everything... I'm sure it's simple too and imma feel stupid. :-(
One of my Main Things is Querying WMI, and i get the ERROR '91' for the Label (This is in a For Each Loop) .. But its the same error, its like its makin me Declare Objects..(using Modules)
Label1.Caption = objItem.Antecedent
If Someone Could PLEASE Help me...
Use
form1.label1.caption = a
But make sure form1 is loaded
You get the error because Label1 and Label2, and your other controls for that matter do not exist in the scope of modGet.bas. They can only be referenced (the properties accessed or set), from with the form. The different error you get when you add Dim Label1 As Object is caused because an you defined Label1 as an Object, not as a Label, and an object does not have a Caption property. Unless you have a good reason for putting the GetNum sub in a .bas module move it into the form and it should work.
I modified the second example. It will modify the strings passed into it in a way that when execution passes back to the form you can assign the strings to your textboxes. I am against modifying controls on a form from another module because it goes against the idea of encapsulation.
'modGet.bas
Option Explicit
Public Function GetHello() As String
Dim strHello As String
strHello = "Hello"
GetHello = strHello
End Function
'frmMain.frm
'Option Explicit
Public Sub btnOpen_Click()
Label1.Caption = GetHello()
End Sub
Something a little different.
'MyModule.bas
Public Sub HelloWorld ByRef Value1 As String, ByVal Value2 As String)
On Error GoTo errHelloWorld
Value1 = "Hello"
Value2 = "World"
Exit Sub
errHelloWorld:
' deal with the error here
End Sub
'frmMain.frm
Option Explicit
Private Sub frmMain_Load()
Dim strText1 As String
Dim strText2 As String
HelloWorld(strText1, strText2)
Text1.Text = strText1
Text2.Text = strText2
End Sub
I also added basic error handling in the second example