Link to home
Start Free TrialLog in
Avatar of Ned_Kelly
Ned_KellyFlag for Australia

asked on

Saving a file with the 'Date and Time' as the file name.

Hi folks, back again.
When I press the cmdEnter in the transaction form I want it to save a text file with the 'Date and Time' as the name of the file, I have tried a range of different code but have not been able to get it to work.
I need to keep a copy of the Transaction as a separate file for every transaction.
The other thing is when it writes to the file it writes the same record thirty times instead of one, as you can see I've a lot to learn.

Private Sub cmdEnter_Click()
Dim strFilename As String
Dim intUserResponse As Integer, intAccNum As Integer, FileName As String
Dim intNew As Integer, i As Integer, intDay As Integer, intMo As Integer, intYr As Integer
Dim udtCredit As CreditStruc, intBal As Integer, NewName As Date



dlgCom.InitDir = "A:\NewName"
dlgCom.FileName = "NewName"

If intUserResponse = vbCancel Then
dlgCom.CancelError = True
End If
dlgCom.Filter = "Text Files(*.txt)|*.txt"
dlgCom.ShowSave
mskAccNum.Text = Val(mskAccNum.Text + 1)
i = FreeFile
Close #i
Open dlgCom.FileName For Output As #i

For intNew = 1 To 30
Write #i, mskAccNum, txtLast, _
mskDeposit, mskWithdrawal, lblDate
Next
Close #i
errHandler:


intBal = frmCredit.mskBalance.Text
Open "A:\Data.dat" For Random As #1 Len = Len(udtCredit)
If mskDeposit.Text <> "" Then
intBal = Val(intBal + mskDeposit.Text)
udtCredit.curBalance = intBal
i = Val(mskAccNum.Text)
Put #1, i, udtCredit
Else
intBal = Val(intBal - mskWithdrawal.Text)
Put #1, i, udtCredit
End If
Close #1
Call ClearFields
mskAccNum.SelStart = 0
mskAccNum.SelLength = Len(mskAccNum.Text)
mskAccNum.SetFocus

End Sub

Thanks for your help.
Ned
ASKER CERTIFIED SOLUTION
Avatar of beckingh
beckingh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike McCracken
Mike McCracken

For intNew = 1 To 30
Write #i, mskAccNum, txtLast, _
mskDeposit, mskWithdrawal, lblDate
Next
Close #i

The above code writes the current record 30 times.  If there are 30 records you need to add code to get the next record of information.  If there is only 1 record or transaction then you only need the write statement.


dlgCom.FileName = "NewName"

The above statement sets the filename to NewName

Try something like

dlgCom.FileName = format(Now,"YYYYMMDD HHMMSS")

the string in the format call is how you want the date time formatted.  You will want to look in help under format to get the format string you want to use.

good luck
mlmcc

Try this one

For intNew = 1 To 30
  Open dlgCom.FileName For Append As #i
  Write #i, mskAccNum, txtLast, _
  mskDeposit, mskWithdrawal, lblDate
  Close #i
Next



suresh podaralla
Avatar of Ned_Kelly

ASKER

Hi Folks, thanks for the info.
You have fixed both problems in a flash, many thanks again.
Is it possible to accept two answers, and share the points.
Regards Ned
I believe you need to contact community support to do that
Good morning.  I have reduced the points for this question to 50, returning the other 50 points to you, Ned.  To complete this transaction as you wish, you can accept one expert's comments here to grade and close, and post a new question within this topic area for the other 50 points.  Please include this link and in the subject of the new question say "Points for __expert name __", in comments, this link.  

 https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20134652

That expert will then comment or propose an answer there for you to transfer the points by grading and closing that one.  Let me know if you have questions.

Moondancer
Community Support Moderator @ Experts Exchange.
Thankyou to everybody for there input.
I have accepted beckingh as the answer.
Ned
sureshpodaralla, please post a comment in the question below which Ned created for your contribution here in this point split.

https://www.experts-exchange.com/jsp/qShow.jsp?qid=20136647

Moondancer
Community Support Moderator @ Experts Exchange