I am new to VBA and learning; however, I am also running VBA on my MAC instead of PC. That being said, I copy the code down just like I have seen in VBA used by PCs and I get a run-time Error '1004'Method 'Range' of object '_Global' failed. Can someone please explain to me why that is?
My code is simple: MonthArray(1) = Range("myMonths").Cells(i,1).value
Thoughts anyone?
Sub Array_OneDimension()
Dim MonthArray(1 To 12) As String
Dim i As Byte
For i = 1 To 12
MonthArray(i) = Range("myMonths").Cells(i, 1).Value 'This is where the error is
Next i
End Sub
I was expecting the code to loop through the range and pull out the value in the locals window.
I developed a front-end for a separate department that would allow them to key in data to our back-end table structure. Part of that front-end form includes a message box which opens each time they make a change. To allow them to provide a reason, add the date, etc.
Only certain users are seeing this message box when they use the form. It's not a permissions issue or a compatibility problem with versions of Access. We've looked into that. We're lost as to what it could be.
I know the code we use for the message box uses a particular library in MS Access. Could that be an issue? Anything else that you folks think it might be? We're just looking for possibilities here.
Source Code:
Option Compare Database
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim cmmnt As String
If Me.NewRecord Then
'Calls modAudit function to record new records to Audit Trail
cmmnt = InputBox("What is the reason for adding this record")
If StrPtr(cmmnt) = 0 Then
Me.Undo
Exit Sub
Else
Call AuditChanges("providerID", "NEW", cmmnt)
End If
Else
'Calls modAudit function to record edits to Audit Trail
cmmnt = InputBox("What is the reason for changing the value of this field?")
If StrPtr(cmmnt) = 0 Then
Me.Undo
Exit Sub
Else
Call AuditChanges("providerID", "EDIT", cmmnt)
End If
End If
End Sub
I'm doing my first project attempting to bind data from an .MDB file to a Windows Form project in Visual Basic. Most of the functions work off an arrays "Hubs", so I wanted to fill this array with information drawn from my database. After using the wizard to create DataSet, I created the following code.
Public Sub LoadData()
For i As Integer = 0 To 15
With Hubs(i)
.ID = HubsDataSet.Tables("Hubs").Rows(i).Item(1).ToString 'This line is flagged as bad by the unit tests.
.City = HubsDataSet.Tables("Hubs").Rows(i).Item(2).ToString
.Lat = HubsDataSet.Tables("Hubs").Rows(i).Item(3).ToString
.Lon = HubsDataSet.Tables("Hubs").Rows(i).Item(4).ToString
End With
Next
'Load ID1ComboBox List
For i As Integer = 0 To 15
ID1ComboBox.Items.Add(Hubs(i).ID)
Next
'Load ID2ComboBox List
For i As Integer = 0 To 15
ID2ComboBox.Items.Add(Hubs(i).ID)
Next
'Set default values for Combo boxes
ID1ComboBox.SelectedIndex = 0
ID2ComboBox.SelectedIndex = 1
End Sub
While the project appears to run and function properly, I now find that all my unit tests fail, throwing an error that no Row 0 exists. In addition, if I attempt to use a for each statement instead of a for loop, the program no longer functions (it skips the lines entirely, since it does not hit a break point I inserted). I'm new to this and very confused what I'm doing wrong.
Im trying to automatically log into a website using a script so our employees don't have to remember the admin password.
Dim IE
Dim Helem
Set IE =CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "http://rentalapp.zillow.com/"
Do While (IE.Busy)
WScript.Sleep 10
LoopSet
Helem = IE.document.getElementByID("formUsername")
Helem.Value ="username"
Set Helem =IE.document.getElementByID("formPassword")
Helem.Value = "password"
Set Helem = IE.document.Forms(0)
Helem.Submit
I've gotten this far from other posts I've seen but I keep getting an error saying:
Line: 10
char: 2 Error:
Object required
code: 800A01A8
source: Microsoft VBScript runtime error
I've researched the code and and it says there is a typo somewhere but for the life of me I can't figure out where.
I'm not too familiar with vbscript but with functions like this I want to become more familiar with it. Please help.
Helem = IE.document.getElementByID("formUsername")
Helem.Value ="username" <= this is line 10
Failed at line 10: looks like you failed to find an element with the ID "formUsername".
Take a look at the source of the page and check your ID is correct.
Helem = IE.document.getElementByID("formUsername")
==>
Set Helem = IE.document.getElementByID("formUsername")
as later in your code.
I need to version control a Microsoft Access 2007 database and application. Currently everything is contained in a single mdb file.
The application includes:
Forms
VBA code
Actual database
I would assume I need to separate the database from the forms/code. I would like to be able to version control the forms/code as text to support version diffs.
At the moment I don't have access to SourceSafe (I heard there may be some access support) so I would prefer a solution that would work with subversion or git.
Access 2007 has a feature where you can split a DB into its Tables/Queries (backend) and Forms/Reports (front-end). Since your question mentions only version controlling the forms and modules, this might be a more elegant solution. I don't know where modules go after the split, so that might be a stumbling block.
Microsoft offers VSTO (Visual Studio Tools for Office), which will let you develop in VS and run version control via any VS plugin (CVS/SVN/VSS/etc.).
Finally, you can just directly connect to Visual Source Safe. This MSKB article has some good information and background to go through, while this Office Online article is designed for getting you up and running.
Ultimately, I would suggest against taking the code out of Access if at all possible. Assuming the VBA editor is your primary development environment, you'll be adding extra steps to your development process that cannot easily be automated. Every change you make will need to be manually exported, diff'd, and stored, and there is no Application.OnCompile event that you could use to export the changes. Even tougher, you'll have to manually import all changed source files from other developers when they do checkins.
I use the code below to extract the vba code from Excel files, you may be able to modify this to extract from Access.
Sub ExtractVBACode(strSource, objFSO, strExportPath, objLogFile)
Dim objExcel
Dim objWorkbook
Dim objVBComponent
Dim strFileSuffix
Dim strExportFolder
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = true
Set objWorkbook = objExcel.Workbooks.Open(Trim(strSource))
strExportFolder = strExportPath & objFSO.GetBaseName(objWorkbook.Name)
If Not objFSO.FolderExists(strExportFolder) Then
objFSO.CreateFolder(strExportFolder)
End If
For Each objVBComponent In objWorkbook.VBProject.VBComponents
Select Case objVBComponent.Type
Case vbext_ct_ClassModule, vbext_ct_Document
strFileSuffix = ".cls"
Case vbext_ct_MSForm
strFileSuffix = ".frm"
Case vbext_ct_StdModule
strFileSuffix = ".bas"
Case Else
strFileSuffix = ""
End Select
If strFileSuffix <> "" Then
On Error Resume Next
Err.Clear
objVBComponent.Export strExportFolder & "\" & objVBComponent.Name & strFileSuffix
If Err.Number <> 0 Then
objLogFile.WriteLine ("Failed to export " & strExportFolder & "\" & objVBComponent.Name & strFileSuffix)
Else
objLogFile.WriteLine ("Export Successful: " & strExportFolder & "\" & objVBComponent.Name & strFileSuffix)
End If
On Error Goto 0
End If
Next
objExcel.DisplayAlerts = False
objExcel.Quit
End Sub
Can you extract the forms as XML perhaps?
I've struggled with this same problem. I originally wrote code very much like the existing answer. The trick is to get all of your modules onto the file system, but that method has some drawbacks. Going that route, you can get your forms and reports out of the VBA Projects, but you can't get them back in. So, I created a library as part of our Rubberduck VBE Add-in. The library I wrote takes care of importing and exporting all of your code to/from the VBA project to/from the repository as you seemlessly push, pull, and commit. It's a free and open source project, so feel free to download and install the latest version.
Here is an example of how the library is used. I'll be adding actual integration with the VBA editor in a future release.
Dim factory As New Rubberduck.SourceControlClassFactory
Dim repo As Rubberduck.IRepository
Dim git As ISourceControlProvider
Dim xl As New Excel.Application
xl.Visible = true
Dim wb As Excel.Workbook
Set wb = xl.Workbooks.Open("C:\Path\to\workbook.xlsm")
' create class instances to work with
Set repo = factory.CreateRepository(wb.VBProject.Name, "C:\Path\to\local\repository\SourceControlTest", "https://github.com/ckuhn203/SourceControlTest.git")
Set git = factory.CreateGitProvider(wb.VBProject, repo, "userName", "passWord")
' Create new branch to modify.
git.CreateBranch "NewBranchName"
' It is automatically checked out.
Debug.Print "Current Branch: " & git.CurrentBranch
' add a new standard (.bas) code module and a comment to that file
wb.VBProject.VBComponents.Add(vbext_ct_StdModule).CodeModule.AddFromString "' Hello There"
' add any new files to tracking
Dim fileStat As Rubberduck.FileStatusEntry
For Each fileStat In git.Status
' fileStat.FileStatus is a bitwise enumeration, so we use bitwise AND to test for equality here
If fileStat.FileStatus And Rubberduck.FileStatus.Added Then
git.AddFile fileStat.FilePath
End If
Next
git.Commit "commit all modified files"
' Revert the last commit, throwing away the changes we just made.
git.Revert