VBA save binary - vba

I have a simple code
FileName = "h:\OutStr.txt"
Dim BA(1) As Byte
BA(0) = 99
BA(1) = 100
Open FileName For Binary Access Write As #1
lWritePos = 1
Put #1, lWritePos, BA
Close #1
After saving, OutStr.txt contains two bytes as expected: 99 and 100.
But the result is different in case I am using a function:
Sub BytesToBinaryFile(out_file_name_, vData_)
Open out_file_name_ For Binary Access Write As #1
lWritePos = 1
Put #1, lWritePos, vData_
Close #1
End Sub
FileName = "h:\OutStr.txt"
Dim BA(1) As Byte
BA(0) = 99
BA(1) = 100
Call BytesToBinaryFile(FileName, BA)
In this case the saved file conatins a sequence of bytes, like:
17 32 1 0 2 0 0 0 0 0 0 0 99 100
Could anybody explain me the difference in the results?
Many thanks in advance!

In
Sub BytesToBinaryFile(out_file_name_, vData_)
both variables are of type Variant.
Declare them properly and it works as expected in the function as well
Option Explicit
Public Sub BytesToBinaryFile(ByVal out_file_name_ As String, ByRef vData_() As Byte)
Open out_file_name_ For Binary Access Write As #1
Const lWritePos As Long = 1
Put #1, lWritePos, vData_
Close #1
End Sub
I recommend always to activate Option Explicit: In the VBA editor go to Tools › Options › Require Variable Declaration.
And declare all variables as good as possible and avoid Variant whenever you can.

Related

I'm trying to Multithread read all lines with 2 Threads to get diffrent Text instead of same?

The problem is when i Multithread to read all lines it somehow reads the same line with the 2 Threads so i can see in my console "hello1" "hello1" but i want to see 1thread."hello1" 2thread."hello2" 1thread."hello3" 2thread. "hello4" and so one how is that Possible?
to read the lines im using this:
For Each element As String In File.ReadAllLines("test.txt")
console.writeline(element)
next
There are probably more efficient ways to do it, but try this...
' Custom "indexes"
Dim t1Count As Integer = 1
Dim t2Count As Integer = 1
' In Thread #1
For Each Element As String In File.ReadAllLines("test.txt")
' Index is odd
If Not t1Count Mod 2 = 0 Then
console.writeline(element)
End If
t1Count += 1
Next
' In Thread #2
For Each Element As String In File.ReadAllLines("test.txt")
' Index is even
If t2Count Mod 2 = 0 Then
console.writeline(element)
End If
t2Count += 1
Next

Readline Error While Reading From .txt file in vb.net

I have a Streamreader which is trowing an error after checking every line in Daycounts.txt. It is not a stable txt file. String lines in it are not stable. Count of lines increasing or decresing constantly. Thats why I am using a range 0 to 167. But
Here is the content of Daycounts.txt: Daycounts
Dim HourSum as integer
Private Sub Change()
Dim R As IO.StreamReader
R = New IO.StreamReader("Daycounts.txt")
Dim sum As Integer = 0
For p = 0 To 167
Dim a As String = R.ReadLine
If a.Substring(0, 2) <> "G." Then
sum += a.Substring(a.Length - 2, 2)
Else
End If
Next
HourSum = sum
R.Close()
End Sub
If you don't know how many lines are present in your text file then you could use the method File.ReadAllLines to load all lines in memory and then apply your logic
Dim HourSum As Integer
Private Sub Change()
Dim lines = File.ReadAllLines("Daycounts.txt")
Dim sum As Integer = 0
For Each line In lines
If line.Substring(0, 2) <> "G." Then
sum += Convert.ToInt32(line.Substring(line.Length - 2, 2))
Else
....
End If
Next
HourSum = sum
End Sub
This is somewhat inefficient because you loop over the lines two times (one to read them in, and one to apply your logic) but with a small set of lines this should be not a big problem
However, you could also use File.ReadLines that start the enumeration of your lines without loading them all in memory. According to this question, ReadLines locks writes your file until the end of your read loop, so, perhaps this could be a better option for you only if you don't have something external to your code writing concurrently to the file.
Dim HourSum As Integer
Private Sub Change()
Dim sum As Integer = 0
For Each line In File.ReadLines("Daycounts.txt")
If line.Substring(0, 2) <> "G." Then
sum += Convert.ToInt32(line.Substring(line.Length - 2, 2))
Else
....
End If
Next
HourSum = sum
End Sub
By the way, notice that I have added a conversion to an integer against the loaded line. In your code, the sum operation is applied directly on the string. This could work only if you have Option Strict set to Off for your project. This setting is a very bad practice maintained for VB6 compatibility and should be changed to Option Strict On for new VB.NET projects

trying to to get specifically ordered data from .txt and then converting and storing it to double or string arrays at visual basic

I am trying, for several days, to take specifically ordered data from a .txt file and then convert and store it to double or string arrays
the data is stored in the file in this way:
1 0 1 0 >= 15
0 1 0 1 >= 28
1 1 0 0 <= 30
0 0 3 1 <= 22
-1 0 2 0 <= 0
(one line after the other with no blank lines between them)
and my code for this goes like:
Using stream As System.IO.FileStream = System.IO.File.OpenRead("C:\Users\user\Desktop\test_new.txt")
Using reader As New System.IO.StreamReader(stream)
Dim lineCount = File.ReadAllLines("C:\Users\user\Desktop\test_new.txt").Length
Dim line As String = reader.ReadLine()
Dim aryTextFile() As String
Dim operator1() As String
Dim variables(,) As Double
Dim results() As Double
Dim counter3 As Integer
counter3 = 0
NRows = lineCount
While (line IsNot Nothing)
Dim columns = line.Split(" ")
aryTextFile = line.Split(" ")
line = reader.ReadLine()
NVars = columns.Length - 3
For j = 0 To UBound(aryTextFile) - 3
variables(counter3, j) = CDbl(aryTextFile(j))
Next j
For j = NVars To UBound(aryTextFile) - 2
operator1(counter3) = CStr(aryTextFile(j))
Next j
For j = UBound(aryTextFile) - 2 To UBound(aryTextFile) - 1
results(counter3) = CDbl(aryTextFile(j))
Next j
counter3 = counter3 + 1
End While
End Using
End Using
I'm getting warnings which result in errors ofc.
Variable 'variables' is used before it has been assigned a value. A null reference exception could result at
runtime. C:\Users\user\Documents\Visual Studio
2008\Projects\WindowsApplication1\WindowsApplication1\Form1.vb 230 25 WindowsApplication1
Variable 'operator1' is used before it has been assigned a value. A null reference exception could result at
runtime. C:\Users\user\Documents\Visual Studio
2008\Projects\WindowsApplication1\WindowsApplication1\Form1.vb 236 25 WindowsApplication1
Variable 'results' is used before it has been assigned a value. A null reference exception could result at
runtime. C:\Users\user\Documents\Visual Studio
2008\Projects\WindowsApplication1\WindowsApplication1\Form1.vb 243 25 WindowsApplication1
So what am I doing wrong and how can I fix it
note: data is saved from dynamic matrixes, so it can be a lot bigger than the displayed example (several lines with several columns), and that is the reason I'm trying to program it, in order to save me some lab time of copying and pasting it manually...
thanks in advance,
Viktor
p.s. if another member or admin can indicate an older post about my question, that would also be very helpful, but I am reading posts for the last 4 days in similar questions and I couldn't find something working for me
p.s.2 since is my first post, I have also tried to attach the project and I couldn't find a way :)
You need to define the dimensions of your arrays before trying to use them.
If you don't know what the size will be use a list instead.
'No defined size - Warning
Dim array1() As String
'Error when trying to access
array1(4) = "Testing"
'Defined size
Dim array2(10) As String
array2(5) = "Testing"

Creating file with QB64

With the following
DIM a AS INTEGER
a = 10
OPEN "myFile" FOR BINARY AS #1
PUT #1, 1, a
CLOSE #1
I get a file (myFile) with two bytes (using QB64). The first byte is indeed 0A, but there is a second byte 00.
How can I create a file with just one byte?
Snip to write a byte to file:
DIM B AS STRING * 1
x = 10
B = CHR$(x)
OPEN "myfile" FOR BINARY AS #1
PUT #1, 1, B
CLOSE #1
Try this
DIM a AS _UNSIGNED _BYTE
a = 10
OPEN "myFile" FOR BINARY AS #1
PUT #1, 1, a
CLOSE #1
It seems like INTEGER is 2 bytes.

Variables in VBA loops

I'm trying to run a VBA application using a loop and using variables whose names depends on where in the loop I am. Specifically something like
Dim i As Integer
i = 1
Dim varname() As String
while i < 50
varname(i) = asdasd
i = i + 1
Wend
Somehow it can't read varname(i) or whatever. It reports subscript out of range.
I have no idea what the problem is, can someone helt me perhaps?
You need to give your array a capacity first.
Sub max()
Dim i As Integer
i = 1
Dim varname() As String
ReDim varname(49) '<---- There
While i < 50
varname(i) = asdasd
i = i + 1
Wend
End Sub
This is a good resource for VBA arrays:
http://msdn.microsoft.com/en-us/library/office/aa164778(v=office.10).aspx