Issue with opening the right path for file dialog - image

i am using vba access 2010 and have a simple form, a button control. the idea is to create a folder and after which use the filedialog to open up the folder i just created. i am able to create the folder, but how do i open up to the path i just created? below will be the code i have, will really really appreciate it if anyone can help. thanks in advance
Option Compare Database
Private Sub Command0_Click()
Dim Foldername As String
Foldername = "\\server\Instructions\"
MkDir ("C:\Users\Stanley\Desktop\New folder\123")
setProfilePicture
End Sub
'----------------------image path setting---------------------
Private Sub setProfilePicture()
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
'Change the contents of the Files of Type list.
'Empty the list by clearing the FileDialogFilters collection.
.Filters.Clear
'Add a filter that includes all files.
.Filters.Add "All files", "*.*"
'Add a filter that includes GIF and JPEG images and make it the first item in the list.
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the action button.
If .Show = -1 Then
'Step through each String in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is a String that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example simply displays the path in a message box.
'MsgBox "Path name: " & vrtSelectedItem
Me.ImagePerson.Picture = vrtSelectedItem
'Me.TextboxPersonFilepath.Value = "File path: " & vrtSelectedItem
Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub
'-----------------end of image path setting--------------

Pass it as a variable.
In your Command0_Click sub, set your directory as a variable, like
X = "MyPath To My Folder"
Then, call setProfilePicture(X)
In setProfilePicture, set it up like:
Private Sub setProfilePicture(MyDir as String)
Then MyDir is now the path to your folder, and you can use it as a variable in your sub

Related

Is there any way to save the checkbox's state of a program made in visual basic 6.0

I have made(designed) a program in Visual Basic 6.0,it consists of around 100 checkboxes ,the program does not require any code just a yes/no checkbox type program , but I want to save the checkbox state ,so that if a check box is in yes state then after restarting the program it's state remains conserved .
I have read about
My.Settings.Save but I dont know how to use it , I am using Visual Basic 6.0.
Create keys in Registry, save each checkbox value on their checkbox Change event and load the status of each of them in the form Initialize event code.
Option Explicit
Private Const MyApp As String = "My Own App" 'put here your application name
Private Const Sett As String = "Settings"
Private Sub CheckBox1_Change()
Dim chkBoxStatus As String
chkBoxStatus = "CheckBox1"
If Me.CheckBox1.value = True Then
SaveSetting MyApp, Sett, chkBoxStatus, CStr(True)
Else
SaveSetting MyApp, Sett, chkBoxStatus, CStr(False)
End If
End Sub
Do the same for all your check boxes.
And then:
Private Sub UserForm_Initialize() 'I do not remember well if VB6 uses Form_Initialize... You must adapt it accordingly.
Dim regValue As String
regValue = GetSetting(MyApp, Sett, "CheckBox1", "No value")
If regValue <> "No value" Then Me.CheckBox1.value = CBool(regValue)
'do the same for all checkboxes in discussion
'.
'.
End Sub
"No value" is returned if no value has been set in Registry (yet)...
Well, I would have made all checkboxes a control array and would save their sate in a text file in some hidden place, like C:\Users\UserName\AppData\Local. It would be a pretty small code.

vb6 Treeview refresh ( show updated file ) in child items

I load treeview1 and treeview2 with same zip names but child items maybe different to each other.
now for the sake of the test i loaded treeview2 with few zip names and it has child items to it, instead of reloading the items i used treeview2.refresh
now leaving the treeview2 items loaded,i open 1 of the zips and add any 1 file in it then going back to treeview2 hit refresh it does not show update unless i reload the items again.
TreeView2.Nodes.Clear
ListFiles app.path & "\folder\", "zip"
ignore the code above i load my items like this ,how do i refresh the items and show updated items without reloading.
Unless you are adding the files to the Zip files through your app, you will have to monitor the content or DateLastModified of the Zip files yourself. You can create a subroutine to iterate through your Zip file names, check their content/DateLastModifed and adjust the nodes in your TreeView. You can call this subroutine on a Timer if the contents of your Zip files changes frequently.
Here's an example of an approach that uses a Dictionary to track each file's LastModifiedDate:
Dim m_objFileModifiedDates As New Dictionary
Dim m_objFSO As New FileSystemObject
Private Sub Command1_Click()
Dim objFolder As Folder
Dim objFile As File
Dim sModifiedFiles As String
Set objFolder = m_objFSO.GetFolder("C:\temp\")
For Each objFile In objFolder.Files
' Check if it exists in Dictionary
If Not m_objFileModifiedDates.Exists(objFile.Name) Then
' Add File
m_objFileModifiedDates.Add objFile.Name, objFile.DateLastModified
Else
' Check Last Modified Date
If m_objFileModifiedDates.Item(objFile.Name) <> objFile.DateLastModified Then
' Update Dictionary
m_objFileModifiedDates.Item(objFile.Name) = objFile.DateLastModified
sModifiedFiles = sModifiedFiles & objFile.Name & vbCrLf
End If
End If
Next
If sModifiedFiles <> "" Then
' Update TreeView with modifed files
MsgBox "Files modified: " & vbCrLf & sModifiedFiles
End If
End Sub
When this detects a file has been modified, you can update your TreeView by reading the modified file's content again and update that TreeView Node, or simply update the whole tree.

HP UFT/QTP 14.00, import CSV and maintain the values in data sheet

i'm importing some data from a csv file, here is the data:
*file.csv
UserName, EmailId, PhoneNumber
Antonio, anto#gmail.com, 1234567890
Oscar, osc#yahoo.com, 9999999999
Luis,lu#hotmail.com,8888888
I have a Function to call this file:
'************************************************************
Function ImportCsvFiletoDatatable(CsvFilePath,SheetName,HeaderDelimiter)
Dim filePath
Dim fso
Dim f
Dim fData
Dim arrData
Dim CsvValue
Dim CsvSheet
Dim CsvFirstLine
Dim CsvColumns
Dim ColumnIndex
Dim rIndex
Dim cIndex
filePath=CsvFilePath 'Specify file Path
'Open CSV File using File System Object
Set fso=createobject("scripting.filesystemobject")
Set f = fso.OpenTextFile(filePath)
CsvFirstLine=f.readline 'Treating like first line is the column names
CsvColumns=split(CsvFirstLine,HeaderDelimiter) 'Split the line using HeaderDelimiter
Set CsvSheet=DataTable.GetSheet(SheetName) 'Get the Specified sheet
'Add the splitted values as Datatable Columns
For ColumnIndex=lbound(CsvColumns) to ubound(CsvColumns)
CsvSheet.addparameter CsvColumns(ColumnIndex),""
Next
While not f.AtEndOfStream
rIndex=f.Line-1 'Specify Row index
fData=f.ReadLine ' Read CSV File Line
arrData=split(fData,",") 'Split the line
cIndex=1 'Specify Column Index
CsvSheet.SetCurrentRow(rIndex) 'Set Row in the Datatable
' Add values in Datatable
For Each CsvValue In arrData
CsvSheet.getparameter(cIndex).value=CsvValue
cIndex=cIndex+1
Next
Wend
f.Close
Set fso=Nothing
End Function
'************************************************************
And works well, but the information is volatile, and i can't manage, or use the data.
Someone know how to keep the data in the data sheet, although leave UFT?
Dim objQtApp, strXlsPath
strXlsPath = Environment("TestDir") & "\Default.xls"
Set objQtApp = CreateObject("QuickTest.Application")
DataTable.Export strXlsPath
objQtApp.Test.DataTable.Import strXlsPath
Set objQtApp = Nothing
The Design Time DataTable is found in the Default.xls. This is loaded when you open a test case or if you edit it manually from UFT. In case you want to refresh it programmatically use the code-snippet above. Export and then with AUtomation Object Import.
Of course put it into a method and call from whatever place is convenient for you.
If you want UFT to take care of it automatically, Create a new Class and a singleton Instance of it.
Implement the Class_Terminate method of the class and put the code there. WHenever UFT exits either because of a crash or or a nomral test run ends, it will try to clean up all Objects created while running. This object will be among them, and as a part of the automatic cleanup process you will save your runtime datatable into the design-time one(Default.xls) and then reload it.

Lists in VBscript return object

I'm writing a vbscript function that looks something like this:
Public Function fnGetXLSFileCount()
Dim fso, src, folder, file, fileList
Set fileList = CreateObject("System.Collections.ArrayList")
Set fso = CreateObject("Scripting.FileSystemObject")
src = "\\myserver\myfolder"
Set folder = fso.GetFolder(src)
For Each file In folder.files
If LCase(fso.GetExtensionName(file)) = "xlsx" Then
fileList.Add file.name
End If
Next
Set fnGetXLSFileCount = fileList
End Function
As you can see I'm creating an ArrayList and then adding all the names of excel files that exist in a specified folder.
I then call this function and use the Set operator to specify that I'm expecting an object to be returned.
Set XLSFileList = fnGetXLSFileCount
When I check the count on the object it seems to be correct.
When I try to pull the names out there is nothing there. What am I doing incorrectly here?
For each file in XLSFileList
name = file.Item(0)
Next
The For Each loop already enumerates the items of the collection. And since you assign just the names to the collection you simply use the loop variable to get the name:
For Each file In XLSFileList
name = file
Next
The Item property can be used to directly access a specific item from the collection:
WScript.Echo XLSFileList.Item(0)

how to send contents of a textbox in vb6 to a notepad document

I am trying to send the data inserted into a textbox into a notepad document so that the data can be held for future reference, as i am trying to create an assessment date reminder as i am a student.
But whenever i try to send the data with this code by writing data = "txtAssign_Name, txtAssign_Due" when i check the notepad after running the program, all that is in the document is txtAssign_Name, txtAssign_Due.
Private Sub Form_Load()
Dim Data As String
txtAssign_Name.Text = ""
txtAssign_Due.Text = ""
Data = ""
Open "F:\Personal\date calender test - Copy\test.txt" For Output As #1
Print #1, Data
Close #1
End Sub
Try this:
Data = txtAssign_Name.Text & ", " & txtAssign_Due.Text
(In the lines above, you will want to NOT set those values to "")
Note - using the built in VB6 writing to text files will result in your output being formated in a way so that VB6 can read it back in again. So all strings will have quotes aroud them, other values are displayed in a variety of odd ways, commas seperate all values.
If the output text file is desired to be primarily for human use, then I find using the richtextbox control a better way of creating the file. Add it from Project -> Components -> Controls -> Microsoft Rich Textbox Control 6.0
Add the Rich text box to your form (you can set visible = false if you don't want users to see it). Add your data to it something like this:
RichTextBox1.text = txtAssingn_Name.text & ", " & txt_Assing_Due.text
' If you need to add more things, add them on to the end like this
RichTextBox1.text = richtextbox1.text & vbcrlf & "More text on the next line"
Then, when all the data is added, you just do this
RichTextBox1.SaveFile "C:\Test.txt", rtfText
rtfText is a visual basic constant, that tells the RTB to save as plain text. The alterntaive is rtfRTF. You can learn more about the RTB on MSDN
OK, so a comment above states that you want to be able to read the data back into VB.
The code as it shown in the question will just output a blank text file, as the variable Data is set to "", so that is all that will be output.
What you need is something like this
private Sub SaveData()
dim SaveFile as integer
SaveFile = Freefile ' This gets a file number not currently in use
Open "F:\Personal\date calender test - Copy\test.txt" For Output As #Savefile
write #SaveFile, txtAssign_Name.Text, txtAssign_Due.Text
close #savefile
end sub
And call SaveData in the appropriate place. NOT form load, as the user won't have entered anything at this point. Either after the user has pressed a button, or on Form_Unload if you want it to autosave on form close.
To populate your form again, something like this
private sub LoadData()
dim LoadFile as integer
dim StringData as string
loadfile = freefile
Open "F:\Personal\date calender test - Copy\test.txt" For Input As #loadfile
input #loadfile, StringData
txtAssign_Name.Text = StringData
input #loadfile, StringData
txtAssign_Name.Text = stringdata
close #loadfile
end sub
You should also add error handing whenever working with files. VB6 is quite particular and its very easy to throw an exception when dealing with files.

Resources