You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Microsoft Access VBA编译错误“Expected: list separator or)”求助

解决PlaySound编译错误:Expected: list separator or )

嗨,别不好意思,刚接触Visual Basic(看起来你用的是VBA,比如Access窗体的事件)的时候,这种小语法坑太容易踩了,我帮你快速搞定这个问题:

问题根源

你写的文件路径N:\Computing\ISDD\Chime.wav是字符串类型,但没有用双引号包裹。VB会把路径里的冒号:当成语句分隔符,误以为你要写两个独立的语句,所以才会抛出“Expected: list separator or )”的错误,并且高亮冒号位置。

修正后的代码

只需要给路径加上双引号,同时注意VB中调用过程的语法(如果不用Call关键字,参数不需要加括号):

Private Sub Form_Open(Cancel As Integer)
    PlaySound "N:\Computing\ISDD\Chime.wav"
End Sub

如果你习惯用括号传参,可以加上Call关键字:

Private Sub Form_Open(Cancel As Integer)
    Call PlaySound("N:\Computing\ISDD\Chime.wav")
End Sub

额外注意事项

如果运行时还提示“子程序或函数未定义”,那是因为你还没声明PlaySound这个Windows API函数。需要在你的模块顶部(所有过程之外)添加以下声明,兼容32位和64位Office:

#If VBA7 Then
    Declare PtrSafe Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As LongPtr, ByVal dwFlags As Long) As Boolean
#Else
    Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Boolean
#End If

这样调整后,代码应该就能正常编译并播放音频了!

内容的提问来源于stack exchange,提问作者Dylan Jack

火山引擎 最新活动