Join 300,327 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,926 people online right now. Registration is fast and FREE... Join Now!
Before, i had an excel spreadsheet that could export data to notepad and transferted data in a text file using VBA.
Now that i use VB6 and a MsFlexgrid to do the job, i want to transfert the vba Script to my VB6 project. That his to transfer the data from the MsFlexgrid to a text file using the same parameters.
CODE
vFieldArray = Array(1, 1, 12, 6)
But i need help on that.
Could you help me please?
In my msflexgrid, i have columns.
Column 1 = H = (1 space total) Column 2 = A = (1 space total) Column3 = Test = (12 space total) Column 4 = John = (6 space total)
When it transfer the data to the txt file, il will transfert on the same line the information like that: HATest________John__ .
Exemple: Between the T from Test and J from John, there's 12 space. etc.
This his my code:
CODE
Dim intRow As Integer, intCol As Integer Dim i As Long, j As Long Dim f As Integer
For i = 1 To intRow For j = 1 To intCol With MSFlexGrid1
f = FreeFile
Open "C:\Users\Jean-Marc\Desktop\avyrex1926.Txt" For Output As f
Print #f, (i - 1) & Space(1)
Close f End With Next Next
MsgBox "End"
Thanks for your help.
This post has been edited by wilder: 12 Apr, 2009 - 12:03 PM
So what is the problem with the transfer "HATest________John__"? Do you want space or some other delimiter between each item? If so, may have to address each array element individually and then concatenate a string.
So what is the problem with the transfer "HATest________John__"?
The problem his that I cant have the result that I want. I must have a problem with the script and I'm new with vb6 and i dont know how to do it.
I have to find a way to transfer the 4 columns with that specification:
CODE
vFieldArray = Array(1, 1, 12, 6)
In the first column, it will only have a 1 digit text. Same in the second column. In the 3rd colums, it could be different, but it will be a 12 digits max. So if the text his less than 12, it will be the text plus the rest of the 12 digit in spaces.
Same for Column 4 but with 6 digits.
Ex: if column 3 = "Text ", it will have for result: Text + 8 spaces. (Text = 4 digit + 8 spaces equal 12)
Thank's
This post has been edited by wilder: 12 Apr, 2009 - 01:26 PM
So you want to pad the string with spaces? I still think that you may have to concatenate the elements. Here is a function I built to pad strings. I modified it from something I found on the net.
CODE
Private Function Pad(varText As Variant, strAlign As String, intLength As Integer) As String
If Len(varText) >= intLength Then 'if the source string is longer than the specified length, return the Length left characters Pad = Left(varText, intLength) ElseIf strAlign = "L" Then 'text aligns left, fill out the right with spaces Pad = varText & String(intLength - Len(varText), " ") Else 'text aligns right, fill out the left with spaces Pad = String(intLength - Len(varText), " ") & varText End If
End Function
so the concatenation with comma delimiter would be:
The concern I would raise is about the underscores padding the array element. Are they really in the array in which case they will have to be stripped out. Use InStr and Left functions to do that.
This post has been edited by June7: 12 Apr, 2009 - 01:44 PM
Private Sub Command3_Click() Dim varText As Variant Dim intLength As Integer Dim Pad As String Dim strAlign As String If Len(varText) >= intLength Then 'if the source string is longer than the specified length, return the Length left characters Pad = Left(varText, intLength) ElseIf strAlign = "L" Then 'text aligns left, fill out the right with spaces Pad = varText & String(intLength - Len(varText), " ") Else 'text aligns right, fill out the left with spaces Pad = String(intLength - Len(varText), " ") & varText End If
End Sub
Now it does not open any txt file. and where should i put the :
Perhaps I was not clear. Pad is a function, use it to modify the data you start with to save in format you want. You call a function from a procedure and it returns something to the procedure. Here is what I understand you want to do, in outline:
1. Initiate an action, possibly by button click 2. Get some data 3. Manipulate data 4. Save data to text file Here is pseudocode for you to build on (the Pad function is ready to use as is):
CODE
Private Sub Command1_Click()
'write some code to get the data you want to manipulate into an array or recordset or if data in flexigrid on form then address the flexigrid as I thought you were originally doing 'perhaps some of your original code
'write code to manipulate the data, here is where you build the string 'use some kind of looping structure to address each line of the array/flexigrid/recordset 'begin loop 'in place of Array1, Array2, Array3, Array4 put your code for addressing each element 'following line builds the string with comma delimiter and strips out the underscores strRow = Array1 & "," & Array2 & "," & Pad(Left(Array3,InStr(Array3,"_")-1),"L",12) & "," & Pad(Left(Array4,InStr(Array4,"_")-1),","L",6) 'code to save strRow to another array/flexigrid/recordset 'end loop 'code to save the modified dataset to text file
End Sub
Private Function Pad(varText As Variant, strAlign As String, intLength As Integer) As String
If Len(varText) >= intLength Then 'if the source string is longer than the specified length, return the Length left characters Pad = Left(varText, intLength) ElseIf strAlign = "L" Then 'text aligns left, fill out the right with spaces Pad = varText & String(intLength - Len(varText), " ") Else 'text aligns right, fill out the left with spaces Pad = String(intLength - Len(varText), " ") & varText End If
End Function
This post has been edited by June7: 13 Apr, 2009 - 08:57 AM
I typed some of my last post in MS Word. Apparently, the quote marks and apostrophes of the font I used didn't translate when I copied/pasted. Did a couple of edits on that post to fix.
This post has been edited by June7: 13 Apr, 2009 - 09:00 AM
Private Function Pad(varText As Variant, strAlign As String, intLength As Integer) As String
If Len(varText) >= intLength Then 'if the source string is longer than the specified length, return the Length left characters Pad = Left(varText, intLength) ElseIf strAlign = "L" Then 'text aligns left, fill out the right with spaces Pad = varText & String(intLength - Len(varText), " ") Else 'text aligns right, fill out the left with spaces Pad = String(intLength - Len(varText), " ") & varText End If
End Function
And after that, for that part, it does not work:
CODE
Private Sub Command1_Click()
'write some code to get the data you want to manipulate into an array or recordset or if data in flexigrid on form then address the flexigrid as I thought you were originally doing 'perhaps some of your original code
'write code to manipulate the data, here is where you build the string 'use some kind of looping structure to address each line of the array/flexigrid/recordset 'begin loop 'in place of Array1, Array2, Array3, Array4 put your code for addressing each element 'following line builds the string with comma delimiter and strips out the underscores strRow = Array1 & "," & Array2 & "," & Pad(Left(Array3,InStr(Array3,"_")-1),"L",12) & "," & Pad(Left(Array4,InStr(Array4,"_")-1),","L",6) 'code to save strRow to another array/flexigrid/recordset 'end loop 'code to save the modified dataset to text file