Getting a file handle; is there an alternative to CreateFile?

Dear Experts,

I am having problems using CreateFile to obtain the handle of a file. Although it usually works, it is sometimes returning -1. Is there an alternate way to obtain the handle of a file in Visual Basic 6?

Thanks!
ttobin333Asked:
Who is Participating?
 
Martin LissOlder than dirtCommented:
This site shows how to do it with the following code:

SetFileDateTime
The function SetFileDateTime is used to set the Create Date, Last Access Date, or Last Modified Date of a specified file.

    
Public Enum FileDateToProcess
    FileDateCreate = 1
    FileDateLastAccess = 2
    FileDateLastModified = 3
End Enum

Public Function SetFileDateTime(FileName As String, _
        FileDateTime As Double, WhichDateToChange As FileDateToProcess, _
        Optional NoGMTConvert As Boolean = False) As Boolean

Open in new window

where FileName is the name of the file to change, FileDateTime is the new time to assign to the file, in standard VB/VBA dddd.tttttt Double variable format, and WhichDateToChange indicates which date/time of the file to change. It is one of the value of the FileDateToProcess enum described above. NoGMTConvert indicates whether FileDateTime should be converted to GMT. Windows stores file times in GMT, and there in nearly all circumstances you should set NoGMTConvert to False. Since Windows displays file times in local time format, the displayed file time will be N hours later (or earlier if you are East of GMT)  than the time passed in as FileDateTime.

Dim FName As String
Dim Result As Boolean
Dim TheNewTime As Double
Dim WhatTime As FileDateToProcess
Dim TheNewDate As Double

'''''''''''''''''''''''''''''''
' Craete the new date and time.
'''''''''''''''''''''''''''''''
TheNewDate = DateSerial(2006, 7, 4)
TheNewDate  = TheNewDate + TimeSerial(12, 34, 56)

FName = "C:\Test.txt"

Result = SetFileDateTime(FileName:=FName, FileDateTime:=TheNewDate, _ 
                         WhichDateToChange:=WhatTime, NoGMTConvert:=False)
If Result = True Then
    Debug.Print "File date/time successfully modified."
Else
    Debug.Print "An error occurred with SetFileDateTime."
End If

Open in new window

0
 
Martin LissOlder than dirtCommented:
What will the handle be used for?
0
 
ttobin333Author Commented:
To set the file time.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
There certainly are other ways to set the file time, but they will not succeed either. CreateFile returns -1 if access is denied, the file does not exist, or any other error occurred. This might result from the share mode used, and the files being in use.
0
 
ttobin333Author Commented:
Great, thanks!
0
 
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0
 
ttobin333Author Commented:
Martin, can you help with my other open question?
0
 
Martin LissOlder than dirtCommented:
If you provide me with the URL I'll take a look.
0
 
ttobin333Author Commented:
0
 
Martin LissOlder than dirtCommented:
Sorry but that's out of my area of expertise.
0
 
ttobin333Author Commented:
OK, thanks for looking!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.